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.

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:

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 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).

Who is online

Users browsing this forum: No registered users and 2 guests

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