Home Links
Home Page
Blocks try... catch... finally... in JScript 5.
Processing of events in language JavaScript
Job with Cookies on JavaScript
Processing of mistakes in PHP
Installation of the password on page
Erroneous methods of promotion of sites
Erroneous methods of promotion of sites
Keywords - the theory
Do not cling to searches!
Adjustment Firewall under ICQ
Adjustment Firewall under ICQ
Safety PHP+MYSQL+Apache
We use base MySQL
Creation of watermarks with help PHP
Twenty six ways of reception of the qualitative traffic on your site.
Unjustified use OOP
Simple way keshirovanija pages
PHP an example of parsing URL for « User Friendly URLs
 

PHP an example of parsing URL for « User Friendly URLs

In many clauses{articles} met the description user friendly urls, but it has been never resulted a simple and understandable example of realization. In this clause{article} such example is resulted.


So. For the beginning I shall result a code of an example, and further we shall in detail disassemble all functions meeting in him.



<? php

header (" http/1.1 200 ok ");

if (empty ($ _server ['redirect_url'])) $p=array ();

else

{$url = $ _ server ['redirect_url'];

if ($url [0] == '/') $url=substr ($url, 1);

if ($url [strlen ($url)-1] == '/') $url=substr ($url, 0,-1);

$url=explode ('/', $url);

$tmp=count ($url)-1;

if ($tmp> 0)

{

$p=array (); $j=0;

for ($i=0; $i <= $tmp; $i ++)

{

if (! empty ($url [$i]))

{

$p [$j] = $url [$i];

$j ++;

}

}

}

else $p=array ();

}

?>


php an example.

The theory. user friendly urls - the technology which helps will get rid of ugly links with parameters (napr.: http: // site.ru/index.php? action=view*id=1*print=true <http: // www.internet-technologies.ru/? url=http%3A%2F%2Fsite.ru%2Findex.php%3Faction%3Dview%26id%3D1%26print%3Dtrue>) and to replace with their more understandable to the person (napr.: http://site.ru/1/print/ <http: // www.internet-technologies.ru/? url=http%3A%2F%2Fsite.ru%2F1%2Fprint%2F>). The basic advantage in that, that links of a "understandable" kind are more easy remembered by the person. An excellent{a different} example of realization of this technology is the site nokia, well and, certainly, ours php wars. At present use of " understandable links » the standard and a parameter of respect for the visitor. It is the integral element "juzabiliti" any site.


Bases of practice. The idea of the given technology is simple. This use instead of standard page 404 special sripta, which would redirect (or itself has deduced{removed}) required contents.


Example. The visitor enters in a browser http://phpwars.net/content/index.html. <http: // www.internet-technologies.ru/? url=http%3A%2F%2Fphpwars.net%2Fcontent%2Findex.html.> Actually on the server there is no catalogue "content" and especially a file "index.html" in him. All occurs so. The web - server tries to open a file "/content/index.html". Not najdja it  he displays page 404 (which as it was already spoken above is specially prepared script). Further job is entered with a script. He sends http heading 200 (the page is found), processes required by the user url and, as for example on php wars, chooses the necessary data from a database both forms page and gives out her  in a browser of the visitor.


In our example it is considered just the very first stages: sending of heading and the analysis url. Our purpose will transform a line entered by the visitor into a file of parameters.


For sending http headings in php function "header" serves. To her we also use right at the beginning, sending heading « http/1.1 200 ok ».


header (" http/1.1 200 ok ");


Further we check on emptiness an element of a super-global file « $ _server ». It is necessary what to find out as have addressed to a script. For example, you would adjust the web - srever that instead of page 404 he displayed index.php. Thus there are possible{probable} two variants. The visitor has gone under the link to the direct link http://phpwars.net/index.php <http: // www.internet-technologies.ru/? url=http%3A%2F%2Fphpwars.net%2Findex.php> or under any understandable link http://phpwars.net/content/index.html. <http: // www.internet-technologies.ru/? url=http%3A%2F%2Fphpwars.net%2Fcontent%2Findex.html.> In the first case the element "redirect_url" will be empty. He will be equal the second "/content/index.html". If the element is empty, we leave a file of parameters empty.


The following step we appropriate{give} value of an element "redirect_url" a variable "$url". It is done{made} that there would be an opportunity further to use this element.


We prepare "$url" for processing. If the first symbol in line is the slash we delete it . It is necessary that at stuffing a file at us would be impossible the first empty element ("cutting" of a file will be is made on slashes).


if ($url [0] == '/') $url=substr ($url, 1);


The following step we delete last slash at the end of a line if he is.


if ($url [strlen ($url)-1] == '/') $url=substr ($url, 0,-1);


Apparently for allocation podstrok in php function "substr" is used. Three parameters are passed her:


Variable from which it is necessary to allocate podstroku

Number{Room} of a symbol with which begins selected podstroka

Number{Room} of last symbol selected podstroki

If to pass the third parameter as the negative number readout will be is made from the end of a line. It is natural, that function returns necessary podstroku in case of success or flase otherwise.


Divide{Share} and dominate.

The following step we divide{share} a line on elements with the help of function "explode". As a separator we use a slash. Apparently to function "explode" it is passed only two parameters:


Variable which should be divided{shared}

The separator on which will be division is made. Notice, that as a separator it can be used not only one symbol, and an any line.

Function returns a file received at division of a line.


$url=explode ('/', $url);


Further we count quantity{amount} of elements in a file with the help of function "count". If at us a zero of elements (the file is empty) we appropriate{give} to our resulting file of parameters also an empty file. Such situation can develop only theoretically, but nevertheless - protected the God protects.


We consider{examine} a situation when we have more than zero of elements. Here all is simple enough. In a cycle we are passed on all file and the next element is not empty we appropriate{give} to him a corresponding element of a resulting file. Some lines of a code sound difficultly, but apparently in it all.



$p=array (); $j=0;

for ($i=0; $i <= $tmp; $i ++)

{

if (! empty ($url [$i]))

{

$p [$j] = $url [$i];

$j ++;

}

}


Result.

That's all. Processing is finished. In result at us the file with processed url has turned out. So for example if the visitor will go under the link http://phpwars.net/content/index.html, <http: // www.internet-technologies.ru/? url=http%3A%2F%2Fphpwars.net%2Fcontent%2Findex.html%2C> that in our file there will be following elements:


$p [0] = "content"; $p [1] = "index.html"; the Further realization user friendly urls already depends on what other technologies are used (mysql for example).