Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

PHP Help with referencing url in code

chrisj

Bronze Coder
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:

PHP:
$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.
And 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
 
Last edited:
So you want to display something on the page if "sub_" is in the URL?

Probably something like:

PHP:
if(stristr($_SERVER['REQUEST_URI']), "sub_" !== false) {
    echo "Found sub_ in URL!";
}

The $_SERVER['REQUEST_URI'] variable gets the URL as it's seen in the browser whereas $_SERVER['PHP_SELF']) gets the filename of the PHP file currently executing.
 
Thanks for your reply, however after putting this on the html page:

PHP:
        <?php
if(stristr($_SERVER['REQUEST_URI']), "sub_" !== false) {
    echo "Found sub_ in URL!";
}
?>

I get a 404 error.

Any additional suggestions are appreciated
 
Thank you that worked.

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

but I have found that this also works:

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


what is the difference between the two? And which one is best to use?
 
Chrisj you can find this out in the PHP manual.
Both look for a match. The top one matches a string to a string, the bottom one matches a regular expression to a string.
I personally don't write them. I have stayed away from writing them my entire career. If one is needed you can look them up on line most of the time..
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom