PHP Hyper-Global variables
$GLOBALS is used to access global variables anywhere, and to store global variables in the name $globals[index], the variable name is the key to the array.
$_server saves information about the header, path, and location of the script.
<?php
echo $_server[' php_self ']. " <br> ";//file name of the current execution script
echo $_server[' server_name ']. " <br> ";//host name of the server where the script is currently running
echo $_server[' Http_host ']. " <br> ";//Host header from the current request
echo $_server[' Http_referer ']. " <br> ";//returns the page from which the connection came from
echo $_server[' Http_user_agent ']. " <br> ";//check what operating system (including version number) to access this, browser (including version number) and user preferences
echo $_server[' Script_name ']. " <br> ";//Current script path
?>
PHP Forms
<form method= "POST" action= "<?php echo $_server[' php_self '];? > ">
Name: <input type= "text" name= "fname" >
<input type= "Submit" >
</form>
<?php
$name = $_request[' fname ');
Echo $name;
?>
Get method
2.php:<a href= "1.php?subject=php&web=w3school.com.cn" >test $GET </a>
1.php:
<?php
echo "Study". $_get[' Subject ']. "at". $_get[' web ');
?>
The difference between get,post,request:
The $_get variable accepts all requests sent in a Get mode, and what follows in the browser's address bar
The $_post variable accepts all requests that are sent by post, for example, a form is submitted in Method=post, and PHP handles all the variables that are posted
and $_request supports two different ways of sending requests, that is, post and get, which can be accepted.
Display does not display to see the delivery method, get is displayed in the URL (there is a character limit), post is not displayed in the URL, can pass any number of data (as long as the server support)
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.
The information sent by get is visible to anyone, no more than 2000 characters, and is not suitable for sending sensitive data.
Htmlspecialchars () converts special characters to HTML entities to prevent cross-site scripting attacks (entity characters are case sensitive)
Trim () Removes unnecessary characters from user input data (extra spaces, tabs, line breaks)
Stripslashes () remove backslashes in user input data
<body>
<?php
$name = $email = $gender = $comment = $website = "";
if ($_server["Request_method"]== "POST") {
$name =test_input ($_post[' name ']);
$email =test_input ($_post[' email ');
$website =test_input ($_post[' website ');
$comment =test_input ($_post[' comment ');
$gender =test_input ($_post[' gender ');
}
Function Test_input ($data) {
$data =trim ($data);
$data =stripslashes ($data);
$data =htmlspecialchars ($data);
return $data;
}
?>
<form method= "POST" action= "<?php Echo htmlspecialchars ($_server[' php_self ']);? > ">
Name:<input type= "text" name= "name" ><br>
E-mail:<input type= "text" name= "email" ><br>
Website:<input type= "text" name= "Website" ><br>
Comment:<textarea name= "Comment" rows= "5" cols= "></textarea><br>"
Gender:
<input type= "Radio" name= "Gender" value= "female" >female
<input type= "Radio" name= "Gender" value= "male" >Male<br>
<input type= "Submit" name= "submit" value= "Submit" >
</form>
<?php
echo "echo $name. " <br> ";
echo $email. " <br> ";
echo $website. " <br> ";
echo $comment. " <br> ";
echo $gender. " <br> ";
?>
</body>
Do not know why can not be filtered, the source code on the W3school can not be filtered ... We're going to fix it later.
PHP Form Learning