Bona Fide OS Developer
View unanswered posts | View active topics It is currently Mon Mar 18, 2024 10:12 pm



Post new topic Reply to topic  [ 1 post ] 
 Nanoalgorithms, Snippets, Instructions, Code Debris and More 
Author Message

Joined: Sun Sep 19, 2010 9:45 am
Posts: 28
Post Nanoalgorithms, Snippets, Instructions, Code Debris and More
In this thread I will post micro-progresses readily applicable and implemented, about how to do a certain sub-task for a major system.

We must accept that there's no other way to perform complex projects other than basing on very small progresses that can be combinable, taking into account the limitations of the human mind, which can only handle very few things at a time, and only one main thing at the same time, in a functional way.

Writing these micro-progresses demonstrates that at the end of the day, the overall progress and the amount of concrete achievements is much higher than waiting until completing a big system for organizing this gained knowledge and create a "dictionary" of small tricks of ground truths; and likewise, the learning process and error correction accelerate greatly.

As always, all of the contents of this topic, which I write, will be under a public-domain license, and under the Creative Commons 0 1.0 Universal License (CC0), which is equivalent to public domain with legal acknowledgement.

I begin with a snippet-type example, which happens to be the basis that I will use to redirect examples through IDs passed by URL.




__________________________________________________________

Converting 404 Error into a Non-Error Redirection with PHP (GET only, not POST)


Image

Requirements (2):
-PHP 5+
-A server configured with a custom 404 PHP error page

In PHP/Apache, a 404 error makes us to lose the POST and GET variables.

At least, we can still recover the GET variables through $_SERVER["REDIRECT_QUERY_STRING"] in a manual way, but as far as I know, POST variables are indeed lost with this method. For POST and other cases, maybe it would be better to use mod_rewrite, although that requires to gain experience after much trial, error and training, and that's why this snippet is valuable for those who don't want or cannot handle the complexity of mod_rewrite.

If on the presence of a 404 error we use the PHP function header("Location: /some_URL"); before writing any content for the document to the client, we can convert the404 Not Found error into a non-error redirection condition 301 Moved Permanently or 302 Found.

If to this we add the manual use of $_SERVER["REDIRECT_URL"] to indicate a non-existent script, we can combine all of this to create a search URL or redirector based on document IDs, like for example:

http://126.sytes.net/debree?id=0

Where /debree doesn't exist, but we can still use with this snippet.

As we can see, an advantage of this method is that we can use GET "file names" WITHOUT EXTENSION, and without the need to further configure the server, and with the option to use this trick or not, and only when we need to.

At the beginning of our PHP script for the 404 error, we would have:


Code:
<?php
//Here we will handle the non-existent script
//"/debree":
///
if($_SERVER["REDIRECT_URL"]==="/debree")
{
 //As we can see in the previous IF,
 //$_SERVER["REDIRECT_URL"] only contains the name
 //of the document or script, without taking into account
 //the part of the URL for the server, and neither
 //the GET variables. This is ideal to easily detect
 //whether the name of the non-existent script is the one we look for.
 //
 //If we don't want the script to be case-insensitive,
 //we can change the code for it to be so; but currently being
 //case-sensitive gives us more possibilities than if we weren't.
 ///

 //Create an array that contains each of the URLs for every
 //IDs of "debrees" (a special name for documents that describe
 //small syntactic combination, their value and effects, from
 //a practical source code).
 //
 //The ID 0, the "default" one, will point to the index of our site:
 ///
  $debrees=Array(
                 "/"
                );

 //Here we reorganize manually the GET variables.
 //We replace all of the '&' for '=', and then we split
 //$_SERVER["REDIRECT_QUERY_STRING"] (which contains
 //the GET URL variables) by every '='.
 //
 //With this, we know that the string in the
 //[$x+0] array element contains the name of the GET URL variable, and
 //the [$x+1] array element contains its value.
 //
 //Later we will verify whether there exists really a value for
 //the variable, and if not, we avoid to continue.
 //
 //This implementation needs to be improved later, over time:
 ///
  $z=str_replace("&", "=", $_SERVER["REDIRECT_QUERY_STRING"]);
  $z=explode("=", $z);



 //This variable indicates whether we found the desired "debree".
 //If so, it will be set to TRUE and that will prevent showing
 //the normal contents of the 404 error page.
 //
 //But if we didn't find it, it will remain FALSE and, conveniently,
 //we will display the 404 error.
 ///
  $debree_flag_ok=FALSE;

 //Iterate all of our manual array of variables/values
 //for GET:
 ///
  for($x=0;$x<count($z);$x+=2)
  {
   //Here we are interested in finding the GET variable named "id":
   ///
    if(strcmp($z[$x],"id")===0)
    {
     //If we find the "id" GET variable, we see if its
     //value exists, checking for it to have more than 0
     //bytes in length; and then we verify that the numerical
     //value is within the existent range of URL in our array of
     //URL records, and also make sure that it is actually a number,
     //using is_numeric (the order of the conditions in this IF
     //is relevant and must be preserved).
     ///
     if(strlen($z[$x+1])>0 && is_numeric($z[$x+1]) && ($z[$x+1]+0)<count($debrees))
     {
      //HERE IS OUR MAIN TRICK.
      //HERE IS OUR MAIN TRICK.
      //HERE IS OUR MAIN TRICK.
      //HERE IS OUR MAIN TRICK.
      //HERE IS OUR MAIN TRICK.
      //The next line converts the 404 error into
      //a redirection:
      ///
        header("Location: ".$debrees[$z[$x+1]]);

      //With this we indicate that we have found our document,
      //and that we shouldn't the normal contents of the 404 error:
      ///
       $debree_flag_ok=TRUE;

      //Stop the loop, for greater efficiency, or
      //at the very least, as a good programming practice:
      ///
       break;
     }
    }
  }

 //If we find our "debree",
 //we stop here to avoid showing
 //the contents of a 404 page, which
 //wouldn't be correct in such condition.
 ///
  if($debree_flag_ok) return;
}
?>




Example of an URL that demonstrates the use of this code, which converts the non-existent script in a valid query URL:

http://126.sytes.net/debree?id=0
http://126.sytes.net/debree?id=0


If instead of redirecting the URL we would like to show valid content in the current page (for instance, to avoid losing the URL originally written), we would need to make a greater effort and create a complete PHP application for the non-existent script, handled from the PHP script of the error 404, with the principles we saw here, and emulating an HTTP code 200 OK, which is something that is more difficult to do than all of the above, but which also can be achieved basically with header("HTTP/1.0 200 OK"); and/or similar things, besides other elements (and still only for GET variables manually recovered and handled).

_________________
Live Development (click image links for full size):
PC 1: ImagePC 2: Image


Thu Jul 26, 2012 12:40 pm
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by Vjacheslav Trushkin and tweaked by the BF Team.