PHP Hyper Global Variables $_get and $_post are used to collect form data (Form-data).
PHP-a simple HTML form
The following example shows a simple HTML form that contains two input fields and a Submit button:
Instance---source code
1<! DOCTYPE html>23<body>4 5<form action="/demo/welcome.php"Method="Post">6Name: <input type="text"Name="name"><br>7E-mail: <input type="text"Name="Email"><br>8<input type="Submit">9</form>Ten One</body> A
Operation Result:
When the user fills out this form and clicks the Submit button, the form data is sent to the php file named "welcome.php" for processing. The form data is sent through the HTTP POST method.
To display the submitted data, you can simply output (echo) all the variables.
The "welcome.php" file is like this:
1 2 <body>34 Welcome <?php echo $_post["name "];?><br>5is: <?php echo $_post["email" ];?>67 </body>8
Output:
is [Email protected]
The same result can be obtained using the HTTP get method:
Instance
1<! DOCTYPE html>23<body>4 5<form action="/demo/welcome_get.php"Method="Get">6Name: <input type="text"Name="name"><br>7E-mail: <input type="text"Name="Email"><br>8<input type="Submit">9</form>Ten One</body> A
Operation Result:
"Welcome_get.php" Is this:
1 2 <body>34 Welcome <?php echo $_get["name "];?><br>5is: <?php echo $_get["email" ];?>67 </body>8
The code above is simple. However, the most important content is missing. You need to validate the form data to prevent the script from appearing vulnerable.
Note: keep an eye on security when working with PHP forms!
This page does not contain any form validators, it only shows us how to send and receive form data.
But later chapters will explain how to improve the security of your PHP forms! Proper security validation of forms is important to protect against hacking and spam!
--------------------------------------------------------------------------------------------------------------- -----------------------------------------
GET vs. POST
Both GET and POST create arrays (for example, array (key = value, Key2 = value2, Key3 = Value3, ...) )。 This array contains key/value pairs, where the key is the name of the form control, and the value is the input data from the user.
Get and POST are treated as $_get and $_post. They are hyper global variables, which means that access to them does not need to be scoped-no special code is required, and you can access them from any function, class, or file.
$_get is an array of variables passed to the current script through a URL parameter.
$_post is an array of variables passed through HTTP POST to the current script.
--------------------------------------------------------------------------------------------------------------- -----------------------------------------
When do I use GET?
Information sent from a form through the GET method is visible to anyone (all variable names and values are displayed in the URL). Get also has a limit on the number of messages sent. Limit the number of characters greater than 2000. However, because variables are displayed in URLs, it is also easier to add pages to bookmarks.
A GET can be used to send non-sensitive data.
Note: You must never use GET to send passwords or other sensitive information!
--------------------------------------------------------------------------------------------------------------- -----------------------------------------
When to use POST?
The information sent from the form through the POST method is not visible to others (all names/values are embedded in the body of the HTTP request), and there is no limit to the number of messages sent.
In addition, POST supports high-level functions, such as multi-part binary input when uploading files to the server.
However, because the variable does not appear in the URL, you cannot add the page to the bookmark.
Tip: Developers prefer POST to send form data.
Next, let's look at how to handle PHP forms safely!
PHP form Processing