Search Engine Friendly PHP Pages

Zend Technologies - Code Gallery Spotlight - Search Engine Friendly PHP Pages
Now that you have a better understanding of the problem, let’s look at the solution. In order for a spider to traverse (and consequently index) a given page, the URL must be free of any appearance of parameters. But if a given page requires parameters to function properly, what can be done? The answer lies in the use of the $PATH_INFO environment variable, which you can convert a URL from…
http://www.zend.com/myscript.php?myvalue=Hello

…to a spider-friendly format:
http://www.zend.com/myscript.php/myvalue/Hello

Notice that the spider-friendly format contains no indication that there are any parameters being passed at all. Rather, it simply looks like we are trying to access the directory on the zend.com site /myscript.php/myvalue/Hello, and any search engine spider that accesses the page won’t have any trouble following the URL. Yet in reality we are executing the script myscript.php.

But what happened to your parameters?
How to GET your hidden data

Now that you have successfully hidden your parameters within what appears to be a directory structure, how do you get them out? Whenever a PHP script is executed with extra path data appended to the end of the filename (as we did in the spider-friendly example above), the Web server creates an environment variable $PATH_INFO containing this information. You can then access this environment variable through PHP automatically, and parse it to retrieve our data. So our earlier URL…

http://www.zend.com/myscript.php/myvalue/Hello

…would populate the $PATH_INFO variable with:

/myvalue/Hello

…from which you can then parse and retrieve the passed information.
Deciphering your data

Now that you know where your parameters are, the next step is to decipher them into a format that PHP can use. Although there is no required method for doing this, I’ll assume that you have formatted your data in the following way:

/var_name/var_data/var2_name/var2_data/…

Using this method, all that is left is to:

* break the provided string every time we encounter a slash (’/')
* create variables to associate the given names (var_name, var2_name, etc.) with their respective values (var_data, var2_data, etc.)

With all of this in mind, let’s look at some real code.
The script

As with many powerful techniques, the code required to create this ability in your scripts is not difficult to develop. The process consists of traversing an array based on the $PATH_INFO, and creating variables based on that data. In the end, the object is to take the URL…

http://www.zend.com/myscript.php/myvalue/Hello

…then use the data provided in the $PATH_INFO variable to construct corresponding variables:

$myvalue = “Hello”
Code flow

* Check for the existence of $PATH_INFO
* Split $PATH_INFO into an array
* If the total number of parameters is even, add an extra empty element at the end to simplify the traversal in the next step
* Traverse array and create variables based on the $PATH_INFO data

< ?php

if(isset($PATH_INFO)) {

$vardata = explode('/', $PATH_INFO);

$num_param = count($vardata);

if($num_param % 2 == 0) {

$vardata[] = '';
$num_param ;
}

for(var $i = 1; $i < $num_param; $i = 2) {

$$vardata[$i] = $vardata[$i 1];
}
}

?>

Filed under Uncategorized |

Leave a Reply


Protected by Comment Guard Pro