Unstoppable Domains

PHP TUTORIAL: index.php?page=yourpage

Spaceship Spaceship
Watch

LogiK

Established Member
Impact
11
Ever wondered how websites have different url navigation?
In this tutorial i will show you how to stop using the boring:
Code:
http://mysite.com/contact.php
http://mysite.com/faq.php

And teach you how to start using:
Code:
http://mysite.com/?page=contact
http://mysite.com/?page=faq

Now, 1st thing you need is a basic template with your header and navigation.
Create all your other files seperately not including the template structure.

Already done that?.. Now interigate this code into the part of your template you want the page to show up in.

PHP:
<?
$page = $_GET['page'];

if($page == "index"){
include"index.php";
}
elseif($page == "contact"){
include"contact.php";
}
elseif($page == "faq"){
include"faq.php";
}
else{ include"index.php";
}
?>

Basicly, Heres the code broken down...

PHP:
<?
$page = $_GET['page'];
This starts the php code and "grabs" whatever the ?page= part of your URL is defined as, so that it can be used in the if/else statements.

PHP:
if($page == "index"){
include"index.php";
}
The above sees if ?page= "index" and if it is, includes your index.php page, if it isnt it moves on to see if your page is something different.

PHP:
elseif($page == "contact"){
include"contact.php";
}
After seeing if your page is set to index and finding out it isnt, this part of the code finds out if the ?page= is set to something else, in this case "contact"... and if it is set to contact, PHP includes your contact.php page.
You can add as many elseif statements as you like, depending on the number of pages you decide to use.

PHP:
else{ include"index.php";
}
?>
After finding out that $page is not equal to any of the pages you have set, the script then includes the default(index.php) page and ends the process.

I hope this tutorial has simplified the ?page=yourpage navigation and how it done, feel free to comment, constructive critacism is welcomed.

Thanks, Declan.
 
Last edited:
2
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
nice tutorial
 
0
•••
Excellent, simple, easy to understand tutorial. Rep added. :)
 
0
•••
Nice tutorial, however you could make it shorter and simpler. :)

PHP:
<?php
if(!$_GET[page]){
include "index.php"; // Page to goto if nothing picked
} else {
include $_GET[page]."php"; // test.php?page=links would read links.php
}
?>

:)
 
0
•••
Not entirely sure, but shouldn't you protect the $_GET to prevent XSS?
 
0
•••
True, but using
PHP:
$page = $_GET[page];
would still allow a XSS attack couldn't it?
 
0
•••
Dale said:
Nice tutorial, however you could make it shorter and simpler. :)

PHP:
<?php
if(!$_GET[page]){
include "index.php"; // Page to goto if nothing picked
} else {
include $_GET[page]."php"; // test.php?page=links would read links.php
}
?>

:)


That ^^^ would not work, because if someone put in page=catmonkeyfish PHP would try & inlcude catmonkeyfish.php, Therefore my way is a tried & tested effective way.
 
0
•••
Unstoppable Domains
Domain Recover
DomainEasy โ€” Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back