PHP Form Validation
This section will introduce how to use PHP to validate form data submitted by the client.
PHP Form Validation
![]() |
We need to consider security when handling PHP forms.
This section will demonstrate secure handling of PHP form data; to prevent hackers and spam, we need to perform data security validation on forms. |
|---|
The HTML form introduced in this section contains the following input fields: required and optional text fields, radio buttons, and a submit button:
The form validation rules above are as follows:
| Field | Validation Rules |
|---|---|
| Name | Required. + can only contain letters and whitespace |
| Required. + must be a valid email address (including '@' and '.') | |
| Website | Optional. If present, it must contain a valid URL |
| Notes | Optional. Multi-line input field (text area) |
| Gender | Required. Must select one |
First, let's look at the pure HTML form code:
Text Field
The "Name", "E-mail", and "Website" fields are text input elements, and the "Comment" field is a textarea. The HTML code is shown below:
“名字”:
E-mail:
网址:
备注:
Radio Button
The "Gender" field is a radio button; the HTML code is shown below:
性别:
女
男
Form Element
The HTML form code is shown below:
This form uses method="post" method to submit data.
|
| What is the $_SERVER["PHP_SELF"] variable?
$_SERVER["PHP_SELF"]是超级全局变量,返回当前正在执行脚本的文件名,与 document root相关。 |
| --- | --- |
Therefore, $_SERVER["PHP_SELF"] sends the form data to the current page, rather than jumping to a different page.
|
| What is the htmlspecialchars() method?
htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体。 预定义的字符是:
What to pay attention to in PHP forms?
The $_SERVER["PHP_SELF"] variable may be exploited by hackers!
When hackers use cross-site scripting HTTP links to attack, the $_SERVER["PHP_SELF"] server variable can also be injected with scripts. The reason is that cross-site scripting is appended after the path of the executed file, so the string of $_SERVER["PHP_SELF"] will contain the JavaScript code following the HTTP link.
![]() |
**XSS, also called CSS (Cross-Site Script), is a cross-site scripting attack. A malicious attacker inserts malicious HTML code into a web page; when a user browses that page, the embedded HTML code is executed, achieving the attacker's special purpose.** |
|---|
Specify the following form file name as "test_form.php":
Now, we use the URL to specify the submission address "test_form.php"; the code above is modified as shown below:
This is fine.
However, consider that the user will enter the following address in the browser's address bar:
http://www.runoob.com/test_form.php/%22%3E%3Cscript%3Ealert('hacked')%3C/script%3E
The URL above will be parsed as the following code and executed:
A script tag is added to the code, along with an alert command. When the page loads, this JavaScript code will be executed (the user will see a popup). This is just a simple example to illustrate how the PHP_SELF variable can be exploited by hackers.
Please note, any JavaScript code can be added to