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 will be displayed in the browser's address bar), and the amount of information that can be sent is also limited.

Example

The form.html file code is as follows:

<html>
<head>
<meta charset="utf-8">
<title>Example(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 is as follows:

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

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

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

The above form execution demo:


When to use method="get"?

When using method="get" in an HTML form, all variable names and values will be 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 in favorites. 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