PHP Breadcrumb Links

  • By: Nate Baldwin
  • For: PHP

This tutorial and script will use PHP to read where your page is in the site folder structure and build back links to the pages higher up in the hierarchy. If your pages aren't already PHP pages, you might try using our JavaScript breadcrumb links script instead.

Here's an example of the script output for this page:

To use this script, your page will probably need to have a .php extension (or whatever other extension your server requires). Download the .zip archive of the script from here:

Decompress the .zip archive and upload the backlinks.php file (not in the script folder) to the top level of your site (the web root folder). Then, on the page where the links are to appear (with a .php extension), paste in this PHP code into the source code of your page wherever you'd like the links to appear:

<?php include($_SERVER['DOCUMENT_ROOT']."/backlinks.php"); ?>

...or, if that doesn't work and you have an older version of PHP on the server, try...

<?php include($HTTP_SERVER_VARS['DOCUMENT_ROOT']."/backlinks.php"); ?>

That's about it. You can put the script file in a different directly inside the web root folder, but you'd need to update the path in the code snippet above to match the new location. If you'd like to use CSS to customize the styling of your links, the links are all inserted into a new <div class="backlinks"> container (a <div> with the class "backlinks").

There are a few variables near the top of the backlinks.php file that you can also customize to better control the appearance:

$convert_toSpace = true;
true if script should convert _ in folder names to spaces
$upperCaseWords = true;
true if script should convert lowercase to initial caps
$topLevelName = "HOME";
name of home/root directory
$separator = " &gt; ";
characters(s) to separate links in hierarchy (default is a > with a space on either side)