• 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.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP Dynamic Page Content Based on URL?

Ash

Active Coder
I will be doing some coursework for uni soon but the PHP part will come later on. But I wanted to get a head start so wanted to see if anyone could help with this. Previously, with Python and Flask, I could create a page that would have different content depending on the URL. Is this possible with PHP (no extra third-party stuff)?

For example
https://example.com/page/gta would show info about gta from the database
https://example.com/page/fifa would show info about fifa from the database
 

simong1993

Active Coder
Staff Team
Guardian
Hey :)

What you could do it www.example.com/?page=gta then do

PHP:
<?PHP
     echo $_GET['page'];
?>

this will take the varible page from the url and assign it GTA then you could have PHP load certain parts of the page depening on the varible :)
 
Last edited by a moderator:

Ash

Active Coder
Thank you for that @simong1993. That seems like it would work perfectly for what I am looking for. My plan would be to use the variable to read some data from a database and then display that on the page.
 

simong1993

Active Coder
Staff Team
Guardian
im a bit more awake now so i can help more :D

https://preciousbusinessdirectory.com/catagory.php this is my website and i do what you are looking for, if click W look at the url it now reads, https://preciousbusinessdirectory.com/catagory.php?index=W&town=&catagory= okay i have been lazy and put all of the varibles in there lol but what ive done is my code looks for the Index in the URL and once found it then looks in the database for W, if you change the W to E in the url it will then look for E :D

keep an eye on the URL but then look for Website Designer > R > Rochester can you see the way the URL changes all of this is changing what the database brings, now Click Precious Website Designs, but see at the top it now says https://preciousbusinessdirectory.com/listing.php?company=Precious Website Designs what the code has done is pull $company from the URL then check the database for the company, it then displays every field with that ID.

I hope i have been a bit more helpfull with the answer but if you do need more help just drop me a message :D
 

Ash

Active Coder
Thanks for the explanation, it helps quite a lot. And it's exactly what I'm looking to do as well. I'll be getting started on the PHP side in around a week or two so if I have any questions, I'll send them your way :blush:
 

Ghost

Active Coder
Hey :)

What you could do it www.example.com/?page=gta then do

PHP:
<?PHP
     echo $_GET['page'];
?>

this will take the varible page from the url and assign it GTA then you could have PHP load certain parts of the page depening on the varible :)

I think it is worth mentioning that people have to be very careful with $_GET variables.
With the example you gave, it is possible to turn that into a malicious page...

Imagine the following:
example.com/?page=<script>window.location.href="https://malicious-phishing-or-hack-website-here.com";</script>
This type of URL query can be escaped & URL encoded so that it's not prevented automatically by browsers. Echo'ing out a $_GET variable without checking the type of value first (and sanitizing malicious data) is never a good idea. Similarly, doing some SQL like in the following example is dangerous...

SELECT * FROM accounts WHERE username = $_GET['user']

If you do not sanitize that $_GET variable or check the type, you can be hacked using things like this...

example.com/?page=user OR id > 0

In this last example, the URL query is being used to trick the unprotected SQL query into fetching ALL account data instead of just a specific user.
 

Top