NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > Webmaster Tutorials
Reload this Page PHP TUTORIAL: index.php?page=yourpage

Webmaster Tutorials Instructional webmaster-related how-to's and tutorials.

Advanced Search
0 members in live chat ~  


Closed Thread
 
LinkBack Thread Tools
Old 02-11-2007, 01:27 PM THREAD STARTER               #1 (permalink)
NamePros Regular
Join Date: Aug 2006
Location: Liverpool, England
Posts: 405
LogiK will become famous soon enoughLogiK will become famous soon enough
 



PHP TUTORIAL: index.php?page=yourpage


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.
????: NamePros.com http://www.namepros.com/webmaster-tutorials/293008-php-tutorial-index-php-page-yourpage.html
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 Code:
<?
$page 
$_GET['page'];

if(
$page == "index"){
include
"index.php";
}
elseif(
$page == "contact"){
include
"contact.php";
}
????: NamePros.com http://www.namepros.com/showthread.php?t=293008
elseif(
$page == "faq"){
include
"faq.php";
}
else{ include
"index.php";
}
?>
Basicly, Heres the code broken down...

PHP Code:
<?
$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 Code:
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 Code:
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 Code:
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 by LogiK; 02-11-2007 at 01:36 PM.
LogiK is offline  
Old 02-11-2007, 01:31 PM   #2 (permalink)
NamePros Member
Join Date: Dec 2006
Location: Iraq
Posts: 191
USMC_1833 is on a distinguished road
 


Save a Life
nice tutorial
USMC_1833 is offline  
Old 02-11-2007, 03:15 PM   #3 (permalink)
Senior Member
Join Date: Dec 2005
Posts: 1,604
webfreak has much to be proud ofwebfreak has much to be proud ofwebfreak has much to be proud ofwebfreak has much to be proud ofwebfreak has much to be proud ofwebfreak has much to be proud ofwebfreak has much to be proud ofwebfreak has much to be proud ofwebfreak has much to be proud of
 


Diabetes Cystic Fibrosis
Excellent, simple, easy to understand tutorial. Rep added.
webfreak is offline  
Old 02-14-2007, 09:40 AM   #4 (permalink)
NamePros Member
Join Date: Nov 2005
Location: Atherstone, UK
Posts: 29
Dale is an unknown quantity at this point
 




Nice tutorial, however you could make it shorter and simpler.

PHP Code:
<?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
????: NamePros.com http://www.namepros.com/showthread.php?t=293008
}
?>
__________________
UK TV Channels
Dale is offline  
Old 02-14-2007, 10:00 AM   #5 (permalink)
NamePros Regular
Join Date: Jan 2007
Posts: 206
crazyryan is on a distinguished road
 



Not entirely sure, but shouldn't you protect the $_GET to prevent XSS?
crazyryan is offline  
Old 02-14-2007, 10:04 AM   #6 (permalink)
NamePros Member
Join Date: Nov 2005
Location: Atherstone, UK
Posts: 29
Dale is an unknown quantity at this point
 




True, but using
PHP Code:
$page $_GET[page]; 
would still allow a XSS attack couldn't it?
__________________
UK TV Channels
Dale is offline  
Old 02-14-2007, 10:31 AM THREAD STARTER               #7 (permalink)
NamePros Regular
Join Date: Aug 2006
Location: Liverpool, England
Posts: 405
LogiK will become famous soon enoughLogiK will become famous soon enough
 



Originally Posted by Dale
Nice tutorial, however you could make it shorter and simpler.

PHP Code:
<?php
if(!$_GET[page]){
include 
"index.php"// Page to goto if nothing picked
????: NamePros.com http://www.namepros.com/showthread.php?t=293008
} 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.
LogiK is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 01:00 AM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger