Skip to content

PHP Programming

Computer Programming | Retrieve URL Data With the PHP GET Function

Overview:

When you need to retrieve URL Data, PHP’s $_GET will get the job done.

URL Data

See the ‘URL Request String Example’ at the bottom of the page before proceeding.

A Request is made by typing into a browser, or clicking a link. Once this happens, an HTTP GET Request has been made. It is called a Request, because you are requesting information from a remote computer. In this case, the remote computer exists at google.com, and the file being requested is registration.php.

You can also see that there is a question mark after the registration.php file. This question mark signifies that there is a data payload onboard. This means the remote computer is called at google.com, the registration.php file is requested, and three pieces of data are being sent as follows:

first_name = Tom

last_name = Slateron

class = accounting

The web works in a Request/Response cycle. A request is made from clicking a link or typing a URL string in the browser. A remote computer and file are called. The registration.php file then collects the data from the URL string, and begins processing your request. Once the Request has been processed, a Response is created. This Response is sent back to you in the form of a web page.

You make a REQUEST. The web server processes your request, and sends back a RESPONSE. This is the basic cycle of the web..

How Does the URL Data Get Processed

$_GET is one of many built in super global arrays in PHP. $_GET has one specific function. It retrieves data from a HTTP GET Request, which is the data in URL string.

From the registration.php file being called, it is very easy to retrieve the data from the URL string.

registration.php

// collect the information from the URL string.<br /> // set this data to 3 new variables to hold the data.<br /> $firstName = $_GET[&#8216;first_name&#8217;];<br /> $lastName = $_GET[&#8216;last_name&#8217;];<br /> $class = $_GET[&#8216;class&#8217;];</p> <p>// now since we have the data from the URL string, let&#8217;s process it<br /> // in a very simple way &#8211; let&#8217;s output this data out the screen.<br /> echo $firstName;<br /> echo $lastName;<br /> echo $class;<br /> ?>

That is all there is to it. Any letters, characters or numbers can be sent in the URL string. When it arrives at registration.php that data will be automatically stored in the Super Global Array $_GET, by the PHP engine. Your only task is to retrieve this data from the $_GET array and use it in your program.

Conclusion

The web works on a Request/Response Cycle. Clicking a link or typing a URL string in a browser creates a GET Request. The remote computer that is being called processes the data, and issues a Response. This Response is the web page that you wanted.

In PHP you can retrieve the data from a GET Request (URL String) with the Super Global Array $_GET.

URL Request Example

href=”http://google.com/registration.phpfirst_name=Tom&last_name=Slateron&class=accounting

Paul Markovich is a Lead Technical Trainer.
He has been designing and building custom web sites for entrepreneurs since 1997. He’ll help you get the answers to your burning open source programming questions!

Be Sociable, Share!
    The following two tabs change content below.

    Latest posts by Ray (see all)

    Add Your Comment (Get a Gravatar)