• 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.

Solved - PHP Dynamic Page Content Based on URL?

Status
This thread has been solved! Go to solution…

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

Coder
Jan 23, 2020
45
10
8
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:

simong1993

Coder
Jan 23, 2020
45
10
8
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
 
  • Like
Reactions: Ash

simong1993

Coder
Jan 23, 2020
45
10
8
Yoda how on earth do I get it to work, I pressed the gear symbol and it said BBCode but did nothing
 

Ghost

Active Coder
Apr 19, 2019
407
184
43
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.
 
  • Like
Reactions: Master Yoda
Status
This thread has been solved! Go to solution…