GET and URLs in PHP

When a form is submitted using the GET method, its values are encoded directly in the query string portion of the URL. For example, if you submit the form above by entering user in the List box and choosing to sort by Name in Ascending order, the browser will call up our index.php script with the following URL:

http://example.org/index.php?list=user&orderby=name&direction=asc

As you can see, the data has been encoded and appended to the and of the URL for our script. In order to access the data, we must now use the $_GET superglobal array. Each argument is accessible through an array key of the same name:

<?php
echo $_GET['list'];
?>

You can create arrays by using array notation…

http://example.org/index.php?list=user&order[by]=column&order[dir]=asc

..and then access themusing the following syntax:

<?php
echo $_GET['order']['by'];

echo $_GET['order']['direction'];
?>
Note that, clearly, there is nothing that stops you from creating URLs that already contain
query data—there is no special trick to it, other than the data must be encoded using a 
particular mechanism that, in PHP, is provided by the urlencode() function:
<?php
$data = "Max & Ruby";

echo "http://www.phparch.com/index.php?name=" . urlencode($data);
?>

The PHP interpreter will automatically decode all incoming data for us, so there is no need to execute urldecode() on anything extracted from $_GET.

Next Topic | Previous Topic

Post to Twitter Post to Digg Post to Facebook Post to Google Buzz Send Gmail

Leave a Comment

Your email address will not be published. Required fields are marked *