Dr. Mark Humphrys

School of Computing. Dublin City University.

Online coding site: Ancient Brain

coders   JavaScript worlds

Search:

Free AI exercises


Lab - 404 PHP handler


Implement the 404 PHP handler




Simple version

  1. Find the bad URL:
    • 404.php extracts the bad URL using REDIRECT_URL in $_SERVER

  2. Find the RHS part of the bad URL:
    • Split the bad URL into segments using explode on "/". This makes an array of segments.
    • Simple version: Assume the bad URL was "/~user/file" and just extract the RHS "file" bit.

  3. Make a list of good URLs:
    • Make a pre-built list of all good URLs. Put this list in the file filelist.

  4. Case-insensitive and partial search:
    • Search the segment in the list of good URLs, using a command-line call to something like:
       grep -i segment filelist 
    • This outputs the lines that match.

  5. Make the hit linkable:
    • Simple version: Assume only one hit.
    • Collect the grep output using shell_exec
    • Output something like:
        
      print "<a href=$hit>$hit</a>"; 
      

  


More complex version

  1. Sanitise all user supplied data, including user supplied URL.
    • Make the URL safe for grep by changing it to alphanumeric plus / plus "." for any other character. We use "." because it means "any character" in grep.
    • Try this using preg_replace:
       preg_replace ( "|[^A-Za-z0-9/]|", '.', $string ) 

  2. Allow more complex bad URL: "/~user/dir/dir/file"
    • "explode" gives us an array of all the segments
    • We look at the RHS segments first. So reverse the array. There is a PHP function to do this.
    • Then go through the array with foreach.
    • Search each segment. Collect the return code from grep.
    • Exit when we got a match.

  3. Assume each grep could have multiple matches
    • Split the return into an array
    • Print possibly multiple links


ancientbrain.com      w2mind.org      humphrysfamilytree.com

On the Internet since 1987.      New 250 G VPS server.

Note: Links on this site to user-generated content like Wikipedia are highlighted in red as possibly unreliable. My view is that such links are highly useful but flawed.