-1

I have a page that will show multiple content, depending on what ID the user has in their URL.

e.g:-

If they have accessed https://stackoverflow.com/#cars - I want to show cars only.

If they have accessed https://stackoverflow.com/#trains - I want to show trains only.

I would like to do this in PHP, as I do not want the other information displayed.

I have tried the following, but with no luck:-

<?php
    $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

    if (strpos($url,'cars') !== false) {
        echo 'Car exists.';
    } else {
        echo 'No cars.';
    }
?>

Can anyone please help?

Brad
  • 73
  • 8
  • 1
    I feel like you are doing it wrong. that's why WordPress has posts and categories built in there. you can simply create a page as trains or cars and get posts filtered related to cars or trains to view there. and there are so many plugins available if you want more customized results in there. no need to code it yourself. it's already there. – Rosh_LK Aug 21 '20 at 08:05
  • 1
    Does this answer your question? [Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?](https://stackoverflow.com/questions/940905/can-i-read-the-hash-portion-of-the-url-on-my-server-side-application-php-ruby) – Nico Haase Aug 21 '20 at 08:11

1 Answers1

1

The # part of a URL is an instruction to the browser to navigate to that element of the page as soon as it loads. It's never sent to the server.

If you want to pass information to the server then use query parameters e.g. https://www.example.com?section=trains and then you can use $_GET["section"] in PHP to retrieve the submitted value.

But as someone has noted in the comments, that's a generic way to do this in a web/PHP context, however Wordpress probably has better methods built in to accomplish the specific filtering task you're interested in here.

ADyson
  • 57,178
  • 14
  • 51
  • 63