PC Services


Some PHP Tips and 'Tricks'

Thinking beyond each page and DESIGNING your web site

Tel: 0118 946 3634

linked-in profile facebook profile

   The Company 
   Resources / Examples 
     Parsing Relative URLs 

Making common files access and menus easier

Relative vs Absolute URLs

There are many schools of thought on this, but mine basically boil down to these rules -

  • When linking externally they must be absolute
  • Internally as much as possible they should be relative URLs
  • Whatever happens you will probably need one or two always accessible as absolute in the start of your files

Often I set one or two files always referenced as absolute, so I can move whole trees of the site if necessary and still get things to work the same. The main information you need to know for creating relative URLs is

  • What is the base directory (relative to site root) for this section of the site
  • How many levels down from web site root is the base directory of this section

From this information it becomes easy to create relative URLs and tables to common files. I often have lists of include files that are referenced by an alias in a table, because I have worked out how far and down the directory tree it is in a simple file path splitting operation. Often my includes list look something this in every file on every directory level, no coding nightmares or long absolute URLs.

include $includes[ 'admin' ]; include $includes[ 'search' ]; include $includes[ 'query' ];

These are aliases into a table of URLs, which have had the necessary relative URL additions done automatically. This means if I want to move a section of a website it can be done quickly and easily with minimal file modifications. This also helps with organising my menu trees, so they can be moved quickly and easily without editing many files, the menus move with the tree and have minor modifications.

How to code these realtive URLs from anywhere on the site

First of all I include a few simple lines at the start of every file, that are coded exactly the same, and make life easier, as follows

$base_dir = ""; include 'base.inc'; $last_modified = date('j<\s\u\p>S</\s\u\p> F Y', filemtime( ( basename( __FILE__ ) ) ) ); include $_SERVER[ 'DOCUMENT_ROOT' ]."/".$base_dir.'global.inc';

  1. First line sets a default condition for our base directory path for this site section. This file ONLY conatins this line when NOT site root directory, as in moved to sub-folder or even different section of the site like order processing or database administration.

    <?php $base_dir="new_base/"; ?>

    Note the traling slash, I prefer this method to leading slash for other uses and allows easy path adding of files or directories in a consistent style.

  2. Second line attempts to include to file base.inc from the current path, so each level has this file in with the same contents, this can easily be copied once modified by a bash script, if file does not exist our default remains in play.
  3. Third line is my standard get last updated for this page setup, for output in footer or anywhere else (see bottom of page
  4. Fourth line gets the global settings for this site section

Note only ONE file has an absolute URL

Next comes the Magic in the global.inc file for THIS site section, here I parse the path of current file and create an array of relative URL prefixes back to our base directory, With this code

$base_level = 0; // Level root of this site is from server root $folders = array( ); if( !isset( $base_level) ) $base_level = 0; // All the folder levels for menu building // tokenise path to get levels and for menus $folders = preg_split( "/\//", $_SERVER[ 'SCRIPT_NAME' ] ); // remove inital empty level if exists if( empty( $folders[ 0 ] ) ) array_shift( $folders ); // levels = number of entries in folders erray 0 = root only $levels = count( $folders ) - 1; // last entry is this module without get or '#' anchors $this_path = ''; for( $loops = 0; $loops < $levels; $loops++ ) $this_path .= $folders[ $loops ].'/'; $this_path .= $folders[ $levels ]; // set relative path to root array for common directory reference // in reverse order matches each level of this file in $folders $forward_level = array( " " ); $str = ""; for( $loops = $levels; $loops > 0; $loops-- ) { $str .= "../"; $forward_level[ ] = $str; } // Final reverse gives [ 0 ] as relative path to root $relative_level = array_reverse( $forward_level ); if( $levels <= 0 ) $relative_level[ 0 ] = ''; if( empty( $relative_level[ $base_level ] ) or $relative_level[ $base_level ] == " " ) $relative_level[ $base_level ] = "./"; // Build array of relative URLs to common files and directories $includes = array( // Starts with include files 'header' => $relative_level[ $base_level ].'inc/header.inc', 'footer' => $relative_level[ $base_level ].'inc/footer.inc', 'css' => $relative_level[ $base_level ].'base.css', 'query' => $relative_level[ $base_level ].'inc/querydbfunc.inc', 'adimin' => $relative_level[ $base_level ].'inc/admin_func.inc', 'search' => $relative_level[ $base_level ].'inc/searchdb.inc', // Menu includes 'root_menu' => $relative_level[ $base_level ].'inc/menu.inc', // General paths 'images' => $relative_level[ $base_level ].'images/', 'photos' => $relative_level[ $base_level ].'photos/, 'js' => $relative_level[ $base_level ].'inc/js/', );

The astute of you will notice the ONLY change required here if the section of the site moves is change $base_level. If standard directories or include files change, they are changed here only and can be referenced by aliases, making coding easier. You need to add some other standard paths or files you can add as many as you like. All references are made to make a relative URL to $base_level for the root of this section of the site.

With this method I have also got relative level URLs for use in menuing systems without having to change URLs for moving or adding sections of the menu and/or site. This means this code makes the menuing references easier - the arrays and variables can be used by other modules.

This has helped make easily portable training sites where a version exists as root of the trainees sub-domain and a copy exists in a sub-directory of a course material examples section looking identical. The only difference being if I need to add links back to the main site I have the variables $base_dir and $base_level to add easy tests for for conditional link outputs.

© 2010 onwards by PC Services, Reading UK Last Updated: 19th July 2013
If you encounter problems with this page please email your comments to webmaster