/    Sign up×
Community /Pin to ProfileBookmark

Help with referencing url in code

I am trying to display something on web pages that show this in the url: “sub__” for example: …com/videos/category/1/sub__536. It was suggested that I try some code like this:

“`
$page = basename($_SERVER[‘PHP_SELF’]);
if ($page==”content.php”) {
echo Hello World;
} elseif ($page==”content.php”) {
Some Other code…
}
“`

but I don’t know where to place the url in that code to make it purposeful.
I’m not sure that I need if/else, just would like if: /videos/category/1/sub__ then show echo.
The code above just gives a 404 error.

Any help/suggestions with display something on web pages that show this in the url: “sub__” is appreciated

to post a comment
PHP

15 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 02.2021 — You may have better luck with $_SERVER['PATH_INFO']:
[code=php]
# looks for a path that ends in "/sub" trailed by 1 or more digits:
if(preg_match('#/sub[0-9]+$#', $_SERVER['PATH_INFO'])) {
echo "We're on a 'sub' page.";
}
[/code]
Copy linkTweet thisAlerts:
@chrisjchrisjauthorNov 03.2021 — Much thanks for your reply.

I put this on the html page, but got a 404 error:
``<i>
</i>&lt;?php
if(preg_match('#/sub[0-9]+$#', $_SERVER['PATH_INFO'])) {
echo "We're on a 'sub' page.";
}
?&gt;<i>
</i>
``

any additional guidance is welcomed.
Copy linkTweet thisAlerts:
@chrisjchrisjauthorNov 03.2021 — Thanks again.

I don't know what to put in the ''PATH_INFO'

Should it be somthing like this:

../videos/category/1/sub__536 ?

but that would only be one subcategory page.

How can it be

../videos/category/1/sub__*

so that any subcategory page displays the echo?

I look forward to any additional assistance
Copy linkTweet thisAlerts:
@NogDogNov 03.2021 — > @chrisjchrisj#1638988 I don't know what to put in the ''PATH_INFO'

$_SERVER['PATH_INFO'] is a built-in global variable, so you don't put anything in it. (See https://www.php.net/manual/en/reserved.variables.server.php for more info.)

> @chrisjchrisj#1638988 so that any subcategory page displays the echo?

That's why I used a regular expression ("regex") within the preg_match() function. For it to match, you need to be clear what is supposed to match. Your original example said it ended in something like /sub536, so the regex I used looks for "/" followed by "sub" followed by one or more numbers (and terminating after those digits). If, in fact, other characters might be in there that have to be accounted for, then you'd need to know what they might be. Or you could just settle for anything that starts with "/sub" by using '#/sub[^/]+$#' as the regex (the [^/]+ means to match on one or more characters that are _not_ a "/"). The specifics of the regex depend on the specifics of what the text could actually be that you want to match.
Copy linkTweet thisAlerts:
@chrisjchrisjauthorNov 03.2021 — Thanks so much for your reply/info. Much appreciated. What you said makes sense, so I'm stumped as to why I don't see the echoed text after placing this on the html page:

``<i>
</i> &lt;?php
if(preg_match('#/sub[0-9]+$#', $_SERVER['PATH_INFO'])) {
echo "We're on a 'sub' page.";
}
?&gt;<i>
</i>
``


any ideas are welcomed.
Copy linkTweet thisAlerts:
@NogDogNov 03.2021 — Start by debugging what's really in the path info:
[code=php]
echo "<pre>PATH_INFO: '{$_SERVER['PATH_INFO']}'</pre>"
[/code]
Copy linkTweet thisAlerts:
@NogDogNov 03.2021 — And if you want to see everything it knows:
[code=php]
echo "<pre>".var_export($_SERVER, 1)."</pre>";
[/code]
Copy linkTweet thisAlerts:
@chrisjchrisjauthorNov 03.2021 — Many thanks again.

When I run this on the html page:

``<i>
</i> &lt;?php
echo "&lt;pre&gt;PATH_INFO: '{$_SERVER['PATH_INFO']}'&lt;/pre&gt;"
?&gt;<i>
</i>
`</CODE>

I see this: PATH_INFO: ' '

And yes, the other<br/>
<C>
echo "<pre>".var_export($_SERVER, 1)."</pre>";`

provides a huge amount of information, wow, like:

'SCRIPT_URL' => '/page_loading.php',

however, when I look at page_loading.php, it's not a big file, and references nothing particular to categories or subcategories.

I look forward to your comments
Copy linkTweet thisAlerts:
@NogDogNov 03.2021 — > @chrisjchrisj#1639018 provides a huge amount of information

Yep. Anything there that looks like it identifies that you're on a "sub" page/request? (i.e., search for "sub"?)

Another possibility might be $_SERVER['REQUEST_URI'] or $_SERVER['ORIG_PATH_INFO'].
Copy linkTweet thisAlerts:
@chrisjchrisjauthorNov 03.2021 — Thanks again, however neither $_SERVER _substitute code displayed the echoed text.

Regarding, ''anything there that looks like it idetifies....."

on line 149 of this script file (and line 215) :

https://pastebin.com/zgbELKJf

has sub_id code.

Also, in the html page there is:

id="sub_categories_"

and also:

&lt;a href="{{LINK videos/{{TYPE}}&lt;?php echo($pt-&gt;page == 'category' &amp;&amp; !empty($_GET['id']) ? '/'.$_GET['id'] : '') ?&gt;&lt;?php echo($pt-&gt;page == 'category' &amp;&amp; !empty($_GET['id']) &amp;&amp; !empty($_GET['sub_id']) ? '/'.$_GET['sub_id'] : '') ?&gt;}}?page_id=1" data-load="?link1=videos&amp;page={{TYPE}}&amp;page_id=1&lt;?php echo($pt-&gt;page == 'category' &amp;&amp; !empty($_GET['id']) ? '&amp;id='.$_GET['id'] : '') ?&gt;&lt;?php echo($pt-&gt;page == 'category' &amp;&amp; !empty($_GET['id']) &amp;&amp; !empty($_GET['sub_id']) ? '&amp;sub_id='.$_GET['sub_id'] : '') ?&gt;" class="waves-effect" title='First Page'&gt;

and this:

``<i>
</i>&lt;?php if ($pt-&gt;page == 'category' &amp;&amp; $pt-&gt;show_sub == true) { ?&gt;
&lt;script type="text/javascript"&gt;
$(document).on('change', '#category_id', function(event) {
event.preventDefault();
id = $(this).val();
$('#sub_category_id').html(sub_categories_array["'"+id+"'"]);
});
$(document).on('change','#sub_categories_', function(event) {
window.location.href = site_url+'/videos/category/&lt;?php echo($_GET['id']) ?&gt;/'+$('#sub_categories_').val();
});
&lt;/script&gt;<i>
</i>
``



I look forward to hearing back from you
Copy linkTweet thisAlerts:
@chrisjchrisjauthorNov 03.2021 — After trial and error, I have found that this works:

if (preg_match('#/?sub__[d]+$#',$_SERVER['REQUEST_URI'])) {

I look forward to your comments
Copy linkTweet thisAlerts:
@NogDogNov 03.2021 — I might change the regex to '#/?sub__[d]+b#' to terminate it on a word boundary (b), just in case anything ever follows it in the URL.
Copy linkTweet thisAlerts:
@chrisjchrisjauthorNov 03.2021 — Many thanks again.

If you don't mind me asking I also saw this:

if(stristr($_SERVER['REQUEST_URI'],"sub_" )!== false) {

and it also works.

what is the difference between that and this:

if (preg_match('#/?sub__[d]+b#',$_SERVER['REQUEST_URI'])) {

and which would be better to use for my ultimate goal to be able to display a search box to allow searching the subcategory page(s) by keyword, something like this:

``<i>
</i>if (preg_match('#/?sub__[d]+b#',$_SERVER['REQUEST_URI'])) {
echo "&lt;form action='#' method='GET' id='sub-search'&gt;
&lt;input id='search' name='keywords' type='text' placeholder='Search SubCategory!'&gt;
&lt;input type='submit' value='Search' /&gt;
&lt;/form&gt;";
}<i>
</i>
``


I look forward to hearing what you think...
Copy linkTweet thisAlerts:
@NogDogNov 03.2021 — Using regular expressions via the preg_*() functions let you do more nuanced/flexible searches. Whether you need that additional power is ultimately your decision based on what you believe the situation will be and what your requirements are. By using the regular expression '#/?sub__[d]+b#' I'm saying it's only a match if you have a "/" immediately followed by the text "sub__" which itself is immediately followed by 1 or more digits, and those are followed by either the end of the string or some non-word character (e.g. if another ?key=value follows it in some cases). If you feel that's overkill, you can certainly use stristr() and maybe save a couple micro-seconds, but I wouldn't base my decision purely on that tiny savings. :)

PS: If you think sub could sometimes be SUB, Sub, etc., just stick an i right after the final # in the regex, and that will make it case-**i**nsensitive.
Copy linkTweet thisAlerts:
@ketchupNov 05.2021 — @NogDog#1639028 fadfadfadfas
×

Success!

Help @chrisjchrisj spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.10,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...