See Blog

Valid XHTML 1.0
Valid CSS

PHP Include Menu Template

PHP Script Determines Current Page

Menu Script: The included menu for this web site uses one 'menu template' file for all pages. It is loaded with SSI (Server Side Include) so only the one include menu file is required. Then the included menu indicates which page is active without using client side scripts (such as JavaScript, Java).

In other words, the php menu script will:

Purpose of this Code: Use one php include menu file that is inserted in Each web page with a SSI (server side include) - then use the php script(s) to determine the current page. Allowing the script to change the content of the included menu so it shows which page is current / active. BTW: The script can be varied in many different ways - such as using button (or sets of buttons) - or using CSS span tags to vary color (perhaps gray out).

Method 1:
Server_Name and Request_URI
determine current file
and/or page name.

This method works by determining the page 'URI' and combining it with the server name to yield a variable with the URL. Read the following menu script then reference the explanation / summary:


$theuri = $_SERVER['REQUEST_URI'];

if ( ($theuri =="/") or ($theuri =="/index.php") )
$itemis = "<img src=\"images/arrow.gif\" alt=\"arrow \">
<span class=\"note\"> Screen Text Here </span>";

} else {

$itemis = "<a href=\"\">Screen Text Here </a>";
echo $itemis;
VIP: Repeat this 'if statement' for each page!

Explanation: The 'theuri' variable combines the server name and the URI to give the ' (such as index.php file name). Then the if statement checks to see if that is the current page - and if so - it adds an image (arrow in this case) - in front of the screen text. It also uses CSS (a 'span' tag) to change the text (grey out - or whatever :)

If the page is NOT the current page, the 'else' statement displays a regular anchor tag (of course this could be a button - or whatever :)

Method 2:
Predefine a User Function
Use Request_URI
to determine folder / directory name

This method works on sites that are organized so that the folders / directories contain separate pages. In other words, if the directory names are like the ones used on this site (e.g. basic - header - scrollbar - etc.) Then the URL will be

With this organized setup, it's easier to define a 'user function' to check each page. This is more powerful but depends on the structure of the site i.e. works well on sites with keywords as the directory names. And when each directory contains a separate web page (such as this site and this page = )


function fixmenu ($name1) {

$name2 = ucfirst($name1);

$item= "<a href=\"/$name1/\">$name2 </a>";

$theur = $_SERVER['REQUEST_URI'];

if ( ($theur =="/$name1/") or ($theur =="/$name1/index.php") )
$item= "<img src=\"/images/arrow.gif\" alt=\"arrow \">$name2";
return $item;

echo fixmenu(directoryname1) . "<br/>Screen Text If Required";

echo fixmenu(directoryname2) . "<br/>Different Screen Text If Required";

echo fixmenu(directoryname3) . "<br/>More Screen Text If Required";


Explanation: This script uses ONE function (over and over).

In the case where all directory names are lower case, a second variable (name2) sets the first character to upper case. Then sets the 'item' variable to a regular link with $name1 andh $name2 as the screen text. (Of course this could be a button or more complicated link code)

Next the URI is read to determine the directory and/or the file in the directory - which is then tested in the 'If Statement'. If the directory name matches - then an image (arrow in this case) is place in front of the screen text. (again this could be expanded to include code to grey out the text or whatever :)

The 'final' variable ($item) is returned to the echo statement that called the function in the first place.

In other words, the 3 example 'echo' lines use the fixmenu function to return 3 differnt items on the included menu. So by adding more 'fixmenu' lines - with different directorynames - you generate the menu items.

P.S. Be Creative - make fancy Dynamic php include menus that show 'What is Happening' :) :) And be sure the code validates :)