Learning points:
1.Header() function
2. Receive and verify data
We are interested in the Web and find it useful because of its ability to post and collect information
primarily through HTML-based forms. These forms are used to encourage website feedback, conduct forum conversations, collect email addresses for online orders, and more.
But encoding an HTML form is only part of what it takes to effectively accept user input, which must be handled by a server-side
component.
1. Header() function
The header is a string sent by the server before transmitting HTML data to the browser using the HTTP protocol. A blank line is required between the
header and the HTML file.
1. Used to redirect the specified URL
<?<span php </span><span header</span>('Location:http://www.baidu.com'<span ); </span>?>
2. Used to set page character encoding
<?<span php </span><span header</span>('Content-Type: text/html; charset=gbk'<span ); </span><span echo</span> '嘿嘿,我是中文!页面编码是GBK,文件也是GBK'<span ; </span>?>
Note: Unless output buffering is enabled, these commands must be executed before any output is returned.
Enable output buffering: ob_start()
<?<span php </span><span ob_start</span><span (); </span>?>
2. Accept and verify data
GET and POST
When processing a form, you must specify how the information entered into the form is transmitted to its destination (method="").
For this, web developers can use GET and POST. When sending data using the GET method, all fields are appended to the browser's URL and the data is sent with the URL address. When using the POST method, the value is sent as a standard
value.
PHP uses $_GET and $_POST superglobal variables to handle GET and POST variables respectively. By using these
two superglobal variables, you can specify exactly where the information should come from and process the data the way you want.
Use $_GET or $_POST to receive data
1.$_GET['username'], the form method sent must be get;
2.$_POST['username'], the form method sent It must be a post;
3. Use isset() to verify whether the $_GET['username'] super global variable is defined;
4. Use the htmlspecialchars() function to filter HTML special characters.
Verify the validity of the data
1. Use the function trim() to remove the leading and trailing spaces of the data;
2. Use the function strlen() to determine the length of the data;
3. Use the function is_numeric() Determine whether the data is pure numbers;
4. Use regular expressions to verify whether the email is legal.
<?<span php </span><span if</span> (!<span isset</span>(<span $_POST</span>['send']) || <span $_POST</span>['send']!='提交'<span ) { </span><span header</span>('Location:Demo1.php'<span ); </span><span exit</span><span ; } </span><span if</span> (<span preg_match</span>('/([\w\.]{2,255})@([\w\-]{1,255}).([a-z]{2,4})/',<span $_POST</span>['email'<span ])) { </span><span echo</span> '电子邮件合法'<span ; } </span><span else</span><span { </span><span echo</span> '电子邮件不合法'<span ; } </span>?>
Note: The article comes from Li Yanhui’s PHP video tutorial. This article is for communication only and may not be used for commercial purposes, otherwise you will be responsible for the consequences.