You should have many links like http://www.domain.com/index.php?page=home OR http://www.domain.com/index.php?page=about where different pages are shown from a single script. [In this case index.php is the script]. Now the question is how to make such kind of system…

Basically, what we can do is, use PHP GET method to pass variable URL to the server and response different pages accordingly. Have a look at the online demo… If, you think you can get the concept from the source code, then here is the link:
Read on below to know how we can implement this…
#1: Strategy behind Coding:
- We shall fetch the page variable from the URL using PHP GET method.
- We shall make a function in which we will pass the page variable using argument.
- Inside the function there will be a switch case to determine what page the client has requested for. We shall use different echo for different page requests…
#2: The main code to produce different page from the same script:
Keeping the above strategy in mind, here is the code… Note that we have assumed the script name to be diff-page.php. Change the links to match your file. Also check the Download Package to know how exactly it works:
<?php
/****************************************
*Different page through Same PHP script using URL Variables by GET method
*@author: Swashata Ghosh
*@email: swashata4u@gmail.com
*@copyright: inTechgrity.com
*@license: Use wherever you want however you can.
*****************************************/
//Main funciton for the Page layout
function layout($page_id) {
switch($page_id) {
default: //Default, ie when the page_id does not match with predefined cases
echo '<p class="red">The page was not found. Showing Home page instead</p>';
case '': //When it is null
case 'home':
echo '<h2>Welcome to the home page</h2>';
echo '<p>This is the home page...</p>';
break;
case 'about':
echo '<h2>Welcome to the about page</h2>';
echo '<p>This is the about page</p>';
break;
case 'contact':
echo '<h2>Welcome to Contact page</h2>';
echo '<p>This is contact page</p>';
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Single index.php PHP script to load different page using URL Variable</title>
</head>
<body>
<p style="text-align: center;"><a href="diff-page.php?page=home">Home Page</a> | <a href="diff-page.php?page=about">About Page</a> | <a href="diff-page.php?page=contact">Contact Page</a></p>
<?php
$page_id = $_GET['page']; //Get the request URL
layout($page_id); //Call the function with the argument
?>
</body>
</html>
That was pretty simple, right? Note that when we pass the URL as diff-page.php?page=contact PHP GETs the $_GET['page'] variable as contact. Then it passes that through the layout function which calls switch to echo relevant pages.
So, the only thing you have to take care is, providing exact file name of the script file [diff-page.php] and the page variables [home, contact, about].
Well that’s it. Although there are many other methods of doing this, but personally I use this as my preferable method. Do give us your feedback and suggestions. Also if you think you know a better algorithm, do share it with us!
Trackbacks/Pingbacks
- Using PHP GET and POST simultaneously via single HTML form | InTechgrity - [...] two great means of passing user data to servers. Previously, we have discussed about how to create different pages ...





Thanks Swashata for this article. I’m already using this strategy for my site where the central content pane of my index.php includes the content form a different php script file for each page the user navigates to by the site links.
But still I can’t figure out how to submit a sitemap for my site ?!!