JavaScript Editor Ajax software     Free javascripts 



Main Page

In the exercise that follows you create a simple affiliate page named
aff_test.php
. When this script
will be loaded with an
aff_id
query string parameter, you’ll retain the affiliate ID value, then remove
the
aff_id
parameter and do a 301 redirect to the new URL.
When removing query string parameters, special care needs to be taken not to alter the already existing
parameters, so you’ll create a few specialized functions that perform these tasks.
Redirecting Dynamic Affiliate URLs
1.
Create a new file named
url_utils.inc.php
in your
seophp/include
folder, and type this
code in:
<?php
// transforms a query string into an associative array
function parse_query_string($query_string)
{
// split the query string into individual name-value pairs
$items = explode(‘&‘, $query_string);
// initialize the return array
$qs_array = array();
// create the array
foreach($items as $i)
{
// split the name-value pair and save the elements to $qs_array
$pair = explode(‘=’, $i);
$qs_array[urldecode($pair[0])] = urldecode($pair[1]);
}
// return the array
return $qs_array;
}
// removes a parameter from the query string
function remove_query_param($url, $param)
{
// extract the query string from $url
$tokens = explode(‘?’, $url);
$url_path = $tokens[0];
$query_string = $tokens[1];
// transform the query string into an associative array
$qs_array = parse_query_string($query_string);
// remove the $param element from the array
unset($qs_array[$param]);
// create the new query string by joining the remaining parameters
$new_query_string = ‘’;
if ($qs_array)
{
foreach ($qs_array as $name => $value)
112
Chapter 5: Duplicate Content
c05.qxd:c05 10:41 112


JavaScript Editor Ajax software     Free javascripts