This tutorial will walk you through creating dynamic pages such as
index.php?page=about
First of all you need the pages you wish to create dynamic paths for. Once having your pages we need to create a new PHP file called index.php
Now having created our new file we can start adding the code to make the pages work.
We need to decide what extension we want the index page to have, in this example I will be using 'page' but you can use anything you wish.
Example: index.php?page=about
The following code starts the PHP file and then looks to see if any page is specified using the GET method and if not it displays the specified file home, again you can change your default file that you want your homepage to be.
The last line of the previous code starts the switch statement to select the correct file.
So we now need to create the list of allowed files to be displayed for the switch statement. Use the following code for each page you want to be able to access, simply copy and paste as many times as needed.
Code Explained:
Then the last thing that is required is to set a default option so if the requested page does not match any of the pages listed above, it will display this page, this script displays a 404 message but you could set it to default to your homepage if you so wish.
The final step is to upload all your pages and this page and try using it by typing your addresses in.
The Full Code:
Hope this is helpful!
index.php?page=about
First of all you need the pages you wish to create dynamic paths for. Once having your pages we need to create a new PHP file called index.php
Now having created our new file we can start adding the code to make the pages work.
We need to decide what extension we want the index page to have, in this example I will be using 'page' but you can use anything you wish.
Example: index.php?page=about
The following code starts the PHP file and then looks to see if any page is specified using the GET method and if not it displays the specified file home, again you can change your default file that you want your homepage to be.
<?php
if (isset($_GET['page'])) $PAGE = $_GET['page'];
else $PAGE = 'home';
switch ($PAGE) {
if (isset($_GET['page'])) $PAGE = $_GET['page'];
else $PAGE = 'home';
switch ($PAGE) {
The last line of the previous code starts the switch statement to select the correct file.
So we now need to create the list of allowed files to be displayed for the switch statement. Use the following code for each page you want to be able to access, simply copy and paste as many times as needed.
//1- index
case 'home':
include ('home.php');
break;
case 'home':
include ('home.php');
break;
Code Explained:
- //1- index - comment for reference purpose
- case 'home': - the page name
- include ('home.php'); - path to the specified file
- break; - shows end of that page
Then the last thing that is required is to set a default option so if the requested page does not match any of the pages listed above, it will display this page, this script displays a 404 message but you could set it to default to your homepage if you so wish.
default:
echo "<p align=center>Error 404!<br><br>The page you request doesn't exist!</p>";
break;
}
?>
echo "<p align=center>Error 404!<br><br>The page you request doesn't exist!</p>";
break;
}
?>
The final step is to upload all your pages and this page and try using it by typing your addresses in.
The Full Code:
<?php
if (isset($_GET['page'])) $PAGE = $_GET['page'];
else $PAGE = 'home';
switch ($PAGE) {
//1- index
case 'home':
include ('home.php');
break;
//2-about
case 'about':
include ('/about/about.htm');
break;
default:
echo "<p align=center>Error 404!<br><br>The page you request doesn't exist!</p>";
break;
}
?>
if (isset($_GET['page'])) $PAGE = $_GET['page'];
else $PAGE = 'home';
switch ($PAGE) {
//1- index
case 'home':
include ('home.php');
break;
//2-about
case 'about':
include ('/about/about.htm');
break;
default:
echo "<p align=center>Error 404!<br><br>The page you request doesn't exist!</p>";
break;
}
?>
Hope this is helpful!