PHP $_GET Variable

There are two ways the browser client can send information to the web server.

  • The GET Method
  • The POST Method

In PHP, the predefined $_GET variable is used to collect values in a form with method="get".

The GET Method

The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? character.

http://www.test.com/index.htm?name1=value1&name2=value2
  • The GET method produces a long string that appears in your server logs, in the browser's Location: box.

  • The GET method is restricted to send upto 1024 characters only.

  • Never use GET method if you have password or other sensitive information to be sent to the server.

  • GET can't be used to send binary data, like images or word documents, to the server.

  • The data sent by GET method can be accessed using QUERY_STRING environment variable.

  • The PHP provides $_GET associative array to access all the sent information using GET method.

 

The $_GET Variable

The predefined $_GET variable is used to collect values in a form with method="get"

Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and has limits on the amount of information to send.

Example

<form action="welcome.php" method="get">
Name: <input type="text" name="fname">
Age: <input type="text" name="age">
<input type="submit">
</form>

When the user clicks the "Submit" button, the URL sent to the server could look something like this:

http://www.w3schools.com/welcome.php?fname=Peter&age=37

The "welcome.php" file can now use the $_GET variable to collect form data (the names of the form fields will automatically be the keys in the $_GET array):

Welcome <?php echo $_GET[“fname”]; ?>.<br>
You are <?php echo $_GET[“age”]; ?> years old!

 

When to use method="get"?

When using method="get" in HTML forms, all variable names and values are displayed in the URL.

Note: This method should not be used when sending passwords or other sensitive information!

However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.

Note: The get method is not suitable for very large variable values. It should not be used with values exceeding 2000 characters.

Both comments and pings are currently closed.

Comments are closed.

Designed for Mytoptutorials.com