[ Team LiB ] Previous Section Next Section

Working with the Query String

The great drawback of the cookie is its dependence on the client. Not only are you at the mercy of the user, who might choose not to allow cookies, but you must also rely on the browser's implementation of the standard. Some browsers have documented bugs concerning the way they deal with cookies. If you want to save state only for a single session, you might decide to use a more traditional approach.

When you submit a form using the GET method, its fields and values are URL encoded and appended to the URL to which the form is sent. They then become available to the server and your scripts. Assuming a form with two fields, user_id and name, the query string should end up looking something like the following:


Each name and value is separated by an equals (=) sign, and each name/value pair is separated by an ampersand (&). PHP decodes this string and makes each of the pairs available in the superglobal $_GET array which stores all arguments submitted via a GET request. So, to access the user_id GET parameter, you would use the $_GET array like this:


You are not limited to using forms to send query strings. You can build your own relatively easily and in so doing pass substantial amounts of information from page to page.

    [ Team LiB ] Previous Section Next Section