Search:

Source | Parameters | Example of usage | Formatting with CSS

SITEMAP

The SITEMAP script dynamically parses a directory tree and generates a corresponding site map. This script provides the generating engine of the site map of jonsson.eu.

The front end of the site map generator is the sitemap($lang) function, which recursively parses the directory pointed out by the DOCUMENT_ROOT server variable for any subdirectories containing a .sitemap file. If a subdirectory contains such a file, then any present text in this file will be read and displayed as a corresponding text tag of the directory in the final site map. For any directories containing a .sitemap file, the parsing will continue in that directory until all valid tags have been found.

For example, the .sitemap tag of the URL you currently are reading (located at http://jonsson.eu/programs/php/sitemap/.sitemap) contains the text


The SITEMAP script dynamically parses a directory tree and
generates a corresponding site map, either as displayed XHTML
or as an XML file to be parsed by search engines. This script
provides the generating engine of the <a href="/sitemap/">
site map</a> of jonsson.eu.

Any subdirectories not containing a .sitemap file will be omitted from the final site map, and the recursive parsing through these directories will be terminated (hence omitting any sub-subdirectories which may contain .sitemap files).

Current revision

Revision 1.4, as of 28/09/2011. Copyright © Fredrik Jonsson 2011, under GPL

Source files

sitemap.php [20 kB] The entire PHP code for the SITEMAP script, containing all necessary routines in one single file. The user-end interface to the SITEMAP script is the function sitemap($lang), as listed at the very end of sitemap.php.
[ download | view source ]

Parameters

Syntax: sitemap($lang [, $mode [, $xmlfile]])

The input argument is as follows.

$lang

Determines the language to use for headers in generation of the quote(s). Accepted values are "en" (for English) or "sv" (for Swedish).

$mode

Optional parameter. Accepted values are the strings "xhtml" or "xml". This parameter determines whether to generate the XHTML code for the display of a sitemap, like in http://jonsson.eu/sitemap/, or if the sitemap instead should be written to an XML file accepted by, for example, Google. The default XML file name is sitemap.xml, written to the site root of the site where the sitemap script resides.

$xmlfile

Optional parameter. This string specifies the file name of the XML sitemap to be written. Unless the second parameter ($mode) was specified as "xml", the $xmlfile is ignored.

In addition to the functional arguments, the sitemap() function scans for a xmlkey parameter supplied via the URL. If the supplied xmlkey string matches the MD5 hash code of the string specified in the customizable parameters section (variable $xml_keystring), then any supplied functional parameters are over-ridden and the default XML site map is generated. This is a convenient way of automatic re-generation of the site map via, for example, a bookmark in your browser, which may be executed whenever changes to the site has been made and you wish to update the sitemap without any fuzz.

Example (this MD5 hash code does not match the actual one which I use in the sitemap script): http://jonsson.eu/sitemap/?xmlkey=25a7551f7a613cf4926fdd89eff66aad If the supplied xmlkey string does not match the expected MD5 hash code, then the attempt of generation of an XML file is ignored and the sitemap() instead generates the regular XHTML for display of the sitemap.

The sitemap($lang) function automatically starts the recursive parsing in the directory pointed out by the DOCUMENT_ROOT variable (in PHP accessed via $_SERVER['DOCUMENT_ROOT']). In case the site map should be generated for a different directory, $_SERVER['DOCUMENT_ROOT'] should in the script be replaced by whatever one instead wish to parse.

Example of usage

Example 1: Generating XHTML for display

In order to invoke SITEMAP for the generation of a site map for display, simply add the following PHP block to an (X)HTML-generating PHP page:


<?php
  include "sitemap.php";
  sitemap("en");
?>

Here, the "en" (for English) may be replaced by "sv" (for Swedish). This is the way the site map of jonsson.eu is generated.

Example 2: Generating XML for search engines - Functional arguments

In order to invoke SITEMAP for the generation of an XML site map, for example supplied to search engines for assistance in indexing a web site, instead change the call to the sitemap() function to:


   
<?php
  include "sitemap.php";
  sitemap("en","xml","/some/directory/mysitemap-20110928.xml");
?>

where "/some/directory/mysitemap-20110928.xml" is the location of the XML file to be written. Notice that in order for PHP to write data to a file, the permissions of the file need to be set as -rw-rw-rw-, for example by running "chmod 666 <mysitemap-20110928.xml> in either a terminal or in FTP.

Example 3: Generating XML for search engines - "xmlkey" switch in URL

Example (this MD5 hash code does not match the actual one which I use in the sitemap script): http://jonsson.eu/sitemap/?xmlkey=25a7551f7a613cf4926fdd89eff66aad

Formatting with Cascaded Style Sheets (CSS)

The site map at http://jonsson.eu/sitemap/ is formatted using the following blocks of CSS code:


/*
 * Define styling of the sitemap of jonsson.eu, at
 * http://jonsson.eu/sitemap/. The (X)HTML blocks for the sitemap are
 * recursively computed by the SITEMAP PHP script, available at
 * http://jonsson.eu/programs/php/sitemap/.
 */
div#sitemap {
  padding: 0px;
  border: 0px;
  margin: 0px;
}

/*
 * Determine the amount of indentation at each level of listing of
 * sub-directories in the site map. Also, set the leftmost containing
 * border of each (unordered) list <ul>...</ul> of subdirectories.
 * I like to keep things arranged this way so that one gets a visual
 * clarity of which blocks belong to which parent directory (which
 * otherwise can be hard if the sitemap is extended way off the
 * current view).
 */
div#sitemap ul {
  list-style: none; /* remove bullets of items in the navigation menu */
  margin-left: 20px;
  border-left: 1px solid #888888;
  border-bottom: 0px solid #888888; /* For a "full-size hook" */
  background: url(/img/hook.png) no-repeat bottom left; /* small hook */
  margin-bottom: 1px;
}

/*
 * The "single line-dot" should be aligned to the very left (0ex) and
 * 0.8ex from the top. As usual, the x-height (ex), or "corpus size",
 * refers to the distance between the baseline and the mean line in a
 * typeface. This way, the site map is styled like:
 *
 *  --* Some directory
 *      |
 *      +--* A sub-directory
 *      |    |
 *      |    +--* A sub-sub-directory
 *      |    |
 *      |    +--* Another sub-sub-directory
 *      |
 *      +--* Another sub-directory
 */
div#sitemap ul li {
  background: url(/img/dot.png) no-repeat 0ex .8ex;
  padding-left: 20px;
}

Here hook.png and dot.png are simple PNG images which are placed as background items to build up the visual appearance of the listed site map.

Return to previous page

Leave a message

Your name:

Your email: (required)

Message:

Generated by ::emailform::

Last modified Wednesday 15 Feb 2023
QR code for string http://jonsson.eu/programs/php/sitemap/