View Full Version : PHP Includes and the Query string Tutorial /?page=1 /?home


Eric
This is a simple tutorial to give you an idea of using PHP includes and the query string to make site management easier.


PHP INCLUDES

Including files with PHP.

For example a file called header.inc contatins:


<html>
<head>
<title>Title of your page</title>
</head>
<body>

<h7>Header Text</h7>


And your main page named index.php contains:

All of your usual html content here.

And a file named footer.inc contains:


Coypright yoursite, All Rights Reserved. and what ever else you want in your footer here.

</body>
</html>


Okay so now we've got three files: header.inc, index.php, and footer.inc

Putting it all together.

In your index.php you can include the header.inc and footer.inc files like this:

File index.php


<?php
include("header.inc");
?>

All of your main html content here.

<?php
include("footer.inc");
?>



Now with the example above the source code would look like this after you call it up in your browser:


<html>
<head>
<title>Title of your page</title>
</head>
<body>

<h7>Header Text</h7>

All of your usual html content here.

Coypright yoursite, All Rights Reserved. and what ever else you want in your footer here.

</body>
</html>



Now what makes this nice is if you want to edit your footer or header file all you have to do is make the change to one file and it will be changed throughout your entire site. It's a lot easier editing only page, especially when you have hundreds or even thousands of pages. That is just a basic example, but it gives you an idea. You can take it a lot further and divide your page into as many sections as you want.

There's a lot more to including files (security) and more which you'll learn eventually with time and experience, however if you don't mind people having the ability to view the files you're including and you're not storing any sensitive information in them this way should be safe.

Some ways to get around people having the ability to view your included files is name them something no one would ever guess. Surrounding them with the start <?php and ?> end tags, and a lot of others you'll eventually learn.

Note: You don't have to use the .inc extension for your included files, you can use .txt, .php, .htm, .html or anything you want.


PHP Includes and the QUERY STRING

Everything that follows the question mark in a url is the query string. The query string works like variable=name and each pair of variable=name is separated by the & sign. The query string can be very useful for creating dynamic web pages. PHP makes it easy to to take advantage of the query string because all the hard stuff has already been done for you.

We'll begin with a simple switch statement to get started and show you how to
make use of the Query String.


File: switch.php


<html>
<body>
<?php

$page = $GET['page'];

switch ($page) {
case '1':
echo 'This is page 1';
break;
case '2':
echo 'This is Page 2';
break;
case '3':
echo 'This is Page 3';
break;
default:
echo "Sorry Page $page doesn't exist";
break;
}

?>
</body>
</html>


Now if you call this page up in your browser:

http://www.yourdomain.com/switch.php?page=1

The result will be:

This is page 1

If you change the end of the url to ?page=2

The result will be:

This is page 2

And for any request that doesn't end with ?page=1, ?page=2, or ?page=3 you'll
get the default.

So if you called up http://www.domain.com/switch.php?page=4

The result would be:

Sorry Page 4 doesn't exist

Now you can change the $page variable to anything you want. So for example if you changed it to $cmd then you would call up the url's like:

http://www.domain.com/switch.php?cmd=1

You can also change each case to what ever you want. So for example you could
change the 1st case to something like case 'design':

Then you could call up http://www.domain.com/switch.php?page=design


You can also include files and do various other things based on the query string rather than just echoing output to the browser.

In this example we will have six separate files.

1. header.inc with the following content:


<html>
<head>
<title>Fun with PHP</title>
</head>
<body bgcolor="#ffffff">


2. main.inc with the following content:

<h7 align="center">This is the main page</h7>

3. design.inc with the following content:

This is the design page

4. services.inc with the following content:

This is the services page

5. footer.inc with following content:


<center>Copyright yoursite.com</center>

</body>
</html>


6. And finally we have our main index.php file with the following content:


<?php
include('header.inc');

$page = $_GET['page'];

switch ($page) {
case 'home':
include('main.inc');
break;
case 'design':
include('design.inc');
break;
case 'services':
include('services.inc');
break;
default:
include('main.inc');
break;
}

include('footer.inc');

?>



Now if you look at the source code after you call the page up in your browser without anything on the query string such as http://yourdomain.com/index.php you'll get the default main.inc file included in the middle of the page. The headers and footers will always remain the same and only the section in the middle where we put the switch statement will change based on the query string.


<html>
<head>
<title>Fun with PHP</title>
</head>
<body bgcolor="#ffffff">

<h7 align="center">This is the main page</h7>

<center>Copyright mysite.com</center>

</body>
</html>


If you change the url to http://www.yourdomain.com/index.php?page=home you'll get the same thing as the default. Because in our switch statement we told it to include the main.inc file if the ?page=home is set in the query string which is the same thing we told it to do for the default, meaning if there's nothing set, then use the default option. You should always be cautious and make sure you include a default or if all else fails option when using includes to prevent malicious users from being able to do something like ?page=your_sensitive_file.

If you change the url to http://www.yourdomain.com/index.php?page=design

You would get this:


<html>
<head>
<title>Fun with PHP</title>
</head>

<body bgcolor="#ffffff">

This is the design page

<center>Copyright mysite.com</center>

</body>
</html>


Now you can see how the design.inc file was included in the main part of the page replacing the main.inc page.

The same will work with http://www.yourdomain.com/index.php?page=services etc.

You can also do the exact same thing using if elseif statements.

Example:


<?php

$page = $_GET['page'];

if ($page == 'design') {
echo 'This is the design page';
} elseif ($page == 'home') {
include('main.inc');
} elseif ($page == 'services') {
include('services.inc');
}
else {
include('main.inc');
}

?>



You can also get rid of the = sign in the query string so that you can call pages up in your browser such http://yourdomain.com/index.php?page1 by using the $_SERVER['QUERY_STRING'] variable.

Example:


<?php
include('header.inc');

$page = $_SERVER['QUERY_STRING'];

switch ($page) {
case 'home':
include('main.inc');
break;
case 'design':
include('design.inc');
break;
case 'services':
include('services.inc');
break;
default:
include('main.inc');
break;
}

include('footer.inc');

?>


Now you could call the page up in your browser such as http://domain.com/index.php?design or http://domain.com/?services or http://domain.com/index.php?home etc.



Using these methods you could manage hundreds or even thousands of pages from a single file. There's a lot of different ways to go about it which you'll learn
eventually if you stick with PHP.

Well that should hopefully give you a basic idea of how you can make site management easier with the use of PHP and the query string. Feel free to correct any mistakes I may have made, post your comments, suggestions, and ask any questions you may have.