Passing Variables – PHP Get – Get it!

Last post we talked about how we normally get data into a system with a form. The user enters his information into the form and clicks the submit button. The browser then sends the data to the server.

Back at the server, php sets aside some memory, the server stores the information in that memory location, and calls the next page. That page is what the developer specified in the form command.

Here's the form again:

In this case, the action parameter tells the server what file to go to next, controller.php.

The more interesting questions are how the browser passes the information to the server, and how the controller.php file gets that information out of memory. Those questions are answered by the method parameter in the form tag, in this case, GET. Get is one of the two methods you can use to get data from the browser to your program file.

The GET method tells the browser to package the site and form data as part of the URL sent to the server calling the next page. For example, if we go to the W3C site at http://www.w3.org/ , and in the Google search box on the right side type, "CSS," click the magnifying glass, and look at the URL at the top of the page.

You'll see: "http://www.google.com/search?q=site:w3.org&q=css&search-submit=" The browser packaged the site "w3.org", the variable data, "css," and the variable, "search," into the URL after the "?"

Now, to get this information into the controller.php file with use the php $_GET method.

And here's how you get the data into the controller.php file:

$searchinfo = $_GET['searchit'];

Notice how the name parameter of the input tag, searchit, is the name of the variable we use to GET the information back out of memory.

if we echo the variable we just created, $searchinfo ,

echo "The search word is: ". $searchinfo;

The answer we get is:

The search word is: css

We've passed the data from the form input page to the controller.php page.

Now some thoughts. The best place to use get is with queries, as shown above, not when your passing sensitive information, like your social security number. Why? For obvious reasons, all your information is in the URL for anybody to see. GET is not secure, but for a search box, it's fast and easy.

Another thing, the URL has a maximum size. Surprising it is mostly set by the browser you use, Internet Explorer is 2048 characters, Firefox is 65,536, Safari is 80,000 and Opera in 190,000. Nothing to really worry about, but its best to keep the "Get" method to short queries back to the server. Get gets a bad rap for messing up the URL line, and putting the data out there for everyone to see. It's not used that much, except for search queries, mostly form data is posted, which we'll talk about next.

Comments are closed.