PHP constants and variables external variables
External variables
PHP’s external variables are some variables that PHP has specified during use. This variable is specified as such and used as such.
Let’s first explain a few of the most commonly used examples. We name the following form user.html:
The above is a very basic HTML code. In the main part of this code It means to use the get method to send the user and password to reg.php (specified in line 6 of the above code). reg.php finds a way to receive the username and pwd values passed by the user.
We get our first external variable: $_GET. The main function of
$_GET is to get the data passed by get.
Let’s write a reg.php and try using $_GET to receive the value:
的值 $u = $_GET['username']; echo $u.'
'; //$_GET['pwd'] 得到表单 的值 $passwd = $_GET['pwd']; echo $passwd.'
'; ?>
You can output the value to see the result. Through the above experiment, we know that the value entered from the form can be obtained through the external variable $_GET.
When you experiment, you will find a feature on the address bar:
According to the picture above, observe the feature:
reg. php is followed by a ? (question mark)
The username in the form changes to the address bar
The username value entered in the form is root, and username is followed by =(etc. number) entered value
username (name) = root (value) The following password is password (name) = 123123 (value), separated by an & (and character) in the middle
The password is Visible, how to ensure safety? What if my password is not visible in the address bar during the registration process?
At this time we need to use post to pass the value. The post value is invisible in the address bar.
We will modify the same code in the above example. The html code is as follows:
$_GET in the PHP code has been changed to $_POST:
的值 $u = $_POST['username']; echo $u.'
'; //$_POST['pwd'] 得到表单 的值 $passwd = $_POST['pwd']; echo $passwd.'
'; ?>
Observation features:
The ? (question mark) after reg.php is missing. The username and password at the back are also missing. So how does he pass the data?
He is the data passed through the request header file of the browser that we cannot see. So the URL column is not visible.
Note: The appendix contains a demonstration process of how to view the transfer results through firebug of the Firefox browser. This part is all the transmission method specified by the HTTP protocol.
In addition, we also have $_REQUEST to receive data. Now we handle it like this:
Change all $_POST in the php code segment to $_REQUEST, the code is as follows:
'; $passwd = $_REQUEST['pwd']; echo $passwd.'
'; ?>
Change the method in this line in the web page user.html to Execute get once, then change it to Post and run it again to see the result: