Freelance Web Designer East Sussex
PHP url parameter and page include
All my tutorials are completely free of charge. If you found them useful and would like to make a donation towards the new tutorials, please click the button below.
I´ve been asked several times how to solve this problem.
Let´s say you have folder which stores files with the content of the pages.
You also have an index.php page from which you send requests via url to display a relevant page. See demo.
This tutorial explains one of the approaches this can be done.
Within your site´s root folder create a folder called pages. Inside of this folder create five pages and provide them with some content:
home.php
about_us.php
services.php
testimonials.php
contact_us.php
In the site´s root folder create file and give it a name of index.php.
Open index.php in any text editor and copy and paste the following content:
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Url parameter</title>
<style type="text/css">
<!--
body {
font-family:Arial, Helvetica, sans-serif;
font-size:10pt;
}
-->
</style>
</head>
<body>
<p style="display:block;padding:0px 0px 20px 0px;"><a href="?pg=home" title="Home">Home</a> | <a href="?pg=about_us" title="About us">About us</a> | <a href="?pg=services" title="Services">Services</a> | <a href="?pg=testimonials" title="Testimonials">Testimonials</a> | <a href="?pg=contact_us" title="Contact us">Contact us</a></p>
</body>
</html>
This piece of code contains the standard html page structure, some styling and navigation which will send a parameter via url. After the closing </p> tag type:
<?php
if (isset($_GET['pg']) && $_GET['pg'] != "") {
$pg = $_GET['pg'];
if (file_exists('pages/'.$pg.'.php')) {
@include ('pages/'.$pg.'.php');
} elseif (!file_exists('pages/'.$pg.'.php')) {
echo 'Page you are requesting doesn´t exist';
}
} else {
@include ('pages/home.php');
}
?>
This simple code does the entire work.
First condition checks if there is a pg parameter passed via url and if it is not empty by using:
if (isset($_GET['pg']) && $_GET['pg'] !="") {
if condition isn´t true (else) then it includes the default, home page:
} else {
@include ('pages/home.php');
}
If condition is true than parameter is assigned to a variable:
$pg = $_GET['pg'];
and another condition checks whether the file with the value of the parameter exists in the pages folder:
if (file_exists('pages/'.$pg.'.php')) {
if it does then this file is included on the page by using:
@include ('pages/'.$pg.'.php');
if file doesn´t exist then the message is displayed
} elseif (!file_exists('pages/'.$pg.'.php')) {
echo 'Page you are requesting doesn´t exist';
}
This is one of many ways you can use include function to populate information from external files by selecting them using url parameter.
Website Design Company | Small Business Web Site Design | Ecommerce Web Site Design | Web Design Tutorials
UK Content Management System | Database Design | Flash Animation | Web Designer Resources | Independent Web Hosting Review
Illustrator Tutorial | Web Design Tutorials | Chichester Restaurants
© Freelance Web Designer - Sebastian Sulinski | Valid CSS | Valid XHTML