Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

PHP Easy Site Navigation

By Joe S
2005-08-16


PHP Easy Site Navigation

This tutorial will show you how to use http://example.com/index.php?p=whatever as navigation for your site.

First you have to create a main page, with a content area that you will include the content with according to the URL.

You may have seen other methods on other websites that use switches to include the page, I dislike that method (switch()) because you have to add a case for each page. Here, all you have to do is make the file, and link to it.

Now, for the code:
Ok, you have your website, and an area where you want to include your different pages. You will put this code instead of the content, and the content will be included from a seperate file - This is a great alternative to iFrames.

<?php // Opening tag for PHP

$p = $_GET['p'];
if ( !empty(
$p) && file_exists('./pages/' . $p . '.php') && stristr( $p, '.' ) == False ) )
{

}

If you're new to PHP, anything after '//' is a comment, and not actual code. Ok - Explanation of the first two lines:
The first line is the opener for PHP code - this is telling the server to parse everything until ?> as PHP code
The second line is a bit more tricky - It's an if statement.
An if statement goes like this: if ( expression ) { statement; }
$p is checking the URL for a "?p=something" - and if it is ! (not) empty, and the file exists in the ./pages/ directory with a PHP extension, and there's no '.' in the request (we don't wand people being able to access files we don't want them to - i.e ?p=../db/mysql.php with your database info), do what is in the curly brackets { }
Now, what it will do...?

$file = './pages/' . $p . '.php';

Variables in PHP are reprisented by a dollar sign ($) followed by a name.
So, If there is a ?p=something in the URL, it will set the $file variable to the file in the ./pages/ directory called something with the extension .php
If you so wish, you can change the folder name, extension, and even what it looks for in the URL ($_GET['p']).
Now, we have to include a default file, if there is no ?p=something in the URL. This is usually the main page (on this website it's the news:)

else
{
   
$file = './pages/default.php';
}

This is saying, that if there isn't a ?p=something, it should set the $file to 'default.php'.
Now, last but not least, lets include the file, and close the PHP code!

else
{
   
$file = './pages/default.php';
}

This tells PHP to include the $file, wheather it's from the URL, or the default page.
Pretty simple, no?

<?php // Opening tag for PHP

$p = $_GET['p'];
if ( !empty(
$p) && file_exists('./pages/' . $p . '.php') && stristr( $p, '.' ) == False ) )
{
   
$file = './pages/' . $p . '.php';
}
else
{
   
$file = './pages/default.php';
}

include
$file;
?>


Tutorial Pages:
» PHP Easy Site Navigation


© 2005 Joey Shamah


 | Bookmark
Related Tutorials:
» Zend Framework Tutorial
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1

Ask A Question
characters left.