PHP $_GETVariable


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


$_GET variable

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

Information sent from a form with the GET method is visible to everyone (it is displayed in the browser's address bar), and there are also limits on the amount of information that can be sent.

Example

The form.html file code is as follows:

<html>
<head>
<meta charset="utf-8">
<title>Ejemplo(example.com)</title>
</head>
<body>

<form action="welcome.php" method="get">
名字: <input type="text" name="fname">
年龄: <input type="text" name="age">
<input type="submit" value="提交">
</form>

</body>
</html>

When the user clicks the "Submit" button, the URL sent to the server looks like this:

http://www.example.com/welcome.php?fname=Ejemplo&age=3

The "welcome.php" file can now collect form data through the $_GET variable (note that the names of the form fields automatically become keys in the $_GET array):

欢迎 <?php echo $_GET["fname"]; ?>!<br>
你的年龄是 <?php echo $_GET["age"]; ?>  岁。

Demonstration of the above form execution:


When to use method="get"?

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

Note:Therefore, this method should not be used when sending passwords or other sensitive information!

However, because the variables are displayed in the URL, the page can be bookmarked. This is useful in some cases.

Note:The HTTP GET method is not suitable for large variable values. Its value cannot exceed 2000 characters.

Other extensions