Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWeb Page and Site Features

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Previous/Next Links With PHP

You've seen the "previous" and "next" links on some web pages with multi-page articles. The site owners are not hand-coding all those links, not if they're smart.

Use the new Previous & Next Links Creator software for a smart and easy implementation.

  1. Upload the PHP script.

  2. Upload a list of URLs.

  3. Paste a line of PHP code into each web page for the previous/next link.

Those are the implementation steps. The script and implementation instructions are further below.

In step 3, the same line of PHP code is pasted into each web page or the CMS page template. That makes Previous & Next Links Creator very friendly to at least some CMS software.

When installed correctly and the web page loads, this happens:

  1. The line of PHP code on the web page calls the PHP script.

  2. The PHP script determines the URL of the web page, then

    • Finds the web page URL on the list of URLs.

    • Constructs links from the previous and the next URLs in the list.

    • Sends the links to the web page.

  3. The web page inserts the previous/next links constructed with the PHP script.

If the current page is at the top of the list, only a "next" link will be published. Similarly, if the current page is last, only a "previous" link.

And, if the current web page URL is not on the list at all, no link will be published. Very CMS friendly. There is no need to be concerned whether or not a web page is on the list of URLs. If it's not, no previous/next links are published.

It's a handy little script.

Here are implementation instructions.

1. Upload the Previous & Next Links Creator PHP Script

Here is the Previous & Next Links Creator script.

<?php
# Previous & Next Links Creator
# Version 1.0
# June 4, 2008
# Copyright 2008, Bontrager Connection, LLC
#
# Before installing or using, you must agree with the
# Multi-Domain Software License Agreement found at
# https://www.willmaster.com/software/MultiDomainLicenseAgreement.php
# or elsewhere on the willmaster.com website.

# Four places can be customized.

# Place 1 --
# Provide the location and name of the file with 
#    the list of page URLs.

$URLfileLocation = $_SERVER['DOCUMENT_ROOT'].'/listofurls.html';

# Place 2 --
# Specify the construction of the "previous" link. Use
#    [[URL]]
#    where the previous page URL shall be inserted.

$PreviousLinkConstruct = '<a href="[[URL]]">&lt; &lt; Prev</a>';

# Place 3 --
# Specify the construction of the "next" link. Use
#    [[URL]]
#    where the next page URL shall be inserted.

$NextLinkConstruct = '<a href="[[URL]]">Next &gt; &gt;</a>';

# Place 4 --
# Type what shall between the "previous" and "next" links. 
#    If nothing, just the single-quotes with nothing between.

$PrevNextLinkSeparator = ' | ';

 /* End of customization section. */
/*********************************/

$LinkConstruct = '';
$URLlines = array();
$URLlines = file($URLfileLocation);
if( count($URLlines) < 1 or ( count($URLlines) < 2 and strlen($URLlines[0]) < 2 ) )
{
   if( strpos('/',$URLfileLocation) === 0 ) { $URLfileLocation = substr($URLfileLocation,1); }
   $URLlines = file($_SERVER['DOCUMENT_ROOT'].'/'.$URLfileLocation);
	if( count($URLlines) < 1 or ( count($URLlines) < 2 and strlen($URLlines[0]) < 2 ) )
   {
      echo("Unable to open $URLfileLocation (or it is empty)");
      exit;
   }
}
$me = $_SERVER['REQUEST_URI'];
if( strpos('?',$me) > 0 ) { $me = preg_replace('/\?.*$/','',$me); }
$mehost = 'http://'.$_SERVER['HTTP_HOST'];
$meurl = "$mehost$me";
$beforeURL = $afterURL = '';
$tlines = array();
foreach($URLlines as $l) { if( preg_match('/\w/',$l) ) { array_push($tlines,$l); } }
$URLlines = $tlines;
$tlines = array();
$lines = count($URLlines);
for($i=0; $i<$lines; $i++)
{
   if( ! strpos($URLlines[$i],'://') ) { $URLlines[$i] = $mehost.$URLlines[$i]; }
   if( ! strpos($URLlines[$i],$me) ) { continue; }
   if( strpos($URLlines[$i],$meurl) === 0 )
   {
      if( preg_match('/<.*>/',$URLlines[$i]) ) { $URLlines[$i] = preg_replace('/<.*>/','',$URLlines[$i]); }
      $URLlines[$i] = preg_replace('/\s*$/','',$URLlines[$i]);
      if( $URLlines[$i] == $meurl )
      {
         if( isset($URLlines[$i-1]) )
         {
            $beforeURL = $URLlines[$i-1];
            if( preg_match('/<.*>/',$beforeURL) ) { $beforeURL = preg_replace('/<.*>/','',$beforeURL); }
            $beforeURL = preg_replace('/\s*$/','',$beforeURL);
            $PreviousLinkConstruct = preg_replace('/\[\[URL\]\]/',$beforeURL,$PreviousLinkConstruct);
            $beforeURL = $PreviousLinkConstruct;
         }
         if( isset($URLlines[$i+1]) )
         {
            $afterURL = $URLlines[$i+1];
            if( preg_match('/<.*>/',$afterURL) ) { $afterURL = preg_replace('/<.*>/','',$afterURL); }
            $afterURL = preg_replace('/\s*$/','',$afterURL);
            $NextLinkConstruct = preg_replace('/\[\[URL\]\]/',$afterURL,$NextLinkConstruct);
            $afterURL = $NextLinkConstruct;
         }
         break;
      }
   }
}
if( $beforeURL ) { echo $beforeURL; }
if( $beforeURL and $afterURL ) { echo $PrevNextLinkSeparator; }
if( $afterURL ) { echo $afterURL; }
?>

Copy the above script, paste it into a plain text processor (like NotePad or TextWrangler), and save it as prevnext.php or other file name that makes sense.

Four places can be customized. Please pay attention to the first one. The rest can be left as is.

The first place that may be customized is the location of the list of URLs that you will upload in step 2. Decide what the file name will be and where on the server it will be located. Then, specify its location (with file name), replacing the /listofurls.html location in the script you copied.

When the customizations have been completed, upload the script to your server. Make a note of the location on the server where the script was uploaded to (you'll need it in step 3).

2. Upload a File with a List of URLs

Create a file containing a list of web page URLs (or URIs, see below) in the order you prefer people to read the pages. Then, upload the file to the location on your server you specified in the script in step 1.

If the web page that will have the "previous" and "next" links is found at the top of the list, no "previous" link will be published. If at the bottom, no "next." And, if not found on the list at all, no link will be published.

A caveat: The URL or the URI of the web page that will have the "previous" and "next" links must match exactly the one on the list.

If your web pages are accessed with both www. and non-www. domain names, then make a list of web page URIs. Otherwise, either URLs or URIs will work so long as, if URL, the domain name matches the one in the site visitor's browser. (A URI is the URL of a web page minus the http:// and domain name part.)

Here is an example list of web page URLs:

http://www.example.com/books/bookone.php
http://www.example.com/books/booktwo.php
http://www.example.com/books/bookthree.php
http://www.example.com/books/bookfour.php
http://www.example.com/books/bookfive.php

And here is an example list of web page URIs:

/books/bookone.php
/books/booktwo.php
/books/bookthree.php
/books/bookfour.php
/books/bookfive.php

3. Paste a Line of PHP Code Into Your Web Pages

The last step is to paste a line of PHP code into each web page (or your web page template) that will have "previous" and "next" links.

Here is the line:

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

No edits will be needed if you uploaded the PHP script file into the document root directory and named the file /prevnext.php   Otherwise, the line of PHP code will need to be edited accordingly.

Paste the line of PHP code into your web pages where you want the "previous" and "next" links to be published.

If the web page is not on the list of URLs uploaded in step 2, no "previous" or "next" links are published on that web page.

Your pages should now be good to go.

It is prudent to test the web pages with the previous/next links to verify they work as they should.

Please use this contact link if you have ideas or suggestions for improving Previous & Next Links Creator. Thank you.

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC