PHP Form Validation
First we will look at the plain HTML code for the form:
When the form is submitted, the form data is sent with method="post".
$_SERVER["PHP_SELF"] sends the form data to the page itself, instead of jumping to a different page. This way, the user will get error messages on the same page as the form.
The htmlspecialchars() function converts special characters to HTML entities. This means that it will replace HTML characters like < and > with < and >. This prevents attackers from exploiting the code by injecting HTML or Javascript code (Cross-site Scripting attacks) in form fields.
When we use the htmlspecialchars() function; then if a user tries to submit the following in a text field:
<script>location.href('http://www.hacked.com')</script>
- this would not be executed, because it would be saved as HTML escaped code, like this:
<script>location.href('http://www.hacked.com')</script>
The code is now safe to be displayed on a page or inside an e-mail.
We will also do two more things when the user submits the form:
We will name the function test_input().
Now, we can check each $_POST variable with the test_input() function, and the script look like this:
Run example »
Notice that at the start of the script, we check whether the form has been submitted using $_SERVER["REQUEST_METHOD"]. If the REQUEST_METHOD is POST, then the form has been submitted - and it should be validated. If it has not been submitted, skip the validation and display a blank form.
However, in the example above, all input fields are optional. The script works fine even if the user do not enter any data.
The next step is to make input fields required and create error messages if needed.
PHP Form Validation Example
* required field.Your Input:
The validation rules for the form above are as follows:| Field | Validation Rules |
|---|---|
| Name | Required. + Must only contain letters and whitespace |
| Required. + Must contain a valid email address (with @ and .) | |
| Website | Optional. If present, it must contain a valid URL |
| Comment | Optional |
| Gender | Required. Must select one |
Text Fields
The name, email, website, and comment fields are text input elements and the HTML code looks like this:
<label>Name:</label><input type="text" name="name">
< label>E-mail:</label><input type="text" name="email">
<label>Website:</label><input type="text" name="website">
<label>Comment:</label><input type="text" name="comment">
< label>E-mail:</label><input type="text" name="email">
<label>Website:</label><input type="text" name="website">
<label>Comment:</label><input type="text" name="comment">
Radio Buttons
The gender fields are radio buttons and the HTML code looks like this:
<label>Gender:</label>
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
The Form Element
The HTML code of the form looks like this:
<form method="post" action="<?php $_SERVER["PHP_SELF"];?>">
$_SERVER["PHP_SELF"] sends the form data to the page itself, instead of jumping to a different page. This way, the user will get error messages on the same page as the form.
Validate Form Data With PHP
The first thing we will do is to pass all variables through PHP's htmlspecialchars() function.The htmlspecialchars() function converts special characters to HTML entities. This means that it will replace HTML characters like < and > with < and >. This prevents attackers from exploiting the code by injecting HTML or Javascript code (Cross-site Scripting attacks) in form fields.
When we use the htmlspecialchars() function; then if a user tries to submit the following in a text field:
<script>location.href('http://www.hacked.com')</script>
- this would not be executed, because it would be saved as HTML escaped code, like this:
<script>location.href('http://www.hacked.com')</script>
The code is now safe to be displayed on a page or inside an e-mail.
| Cross-site scripting (XSS) is a type of computer security vulnerability typically found in Web applications. XSS enables attackers to inject client-side script into Web pages viewed by other users. |
- Strip unnecessary characters (extra space, tab, newline) from the user input data (with the PHP trim() function)
- Remove backslashes (\) from the user input data (with the PHP stripslashes() function)
We will name the function test_input().
Now, we can check each $_POST variable with the test_input() function, and the script look like this:
Example
<?php
// define variables and set to empty values
$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 = $_POST["gender"];
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
// define variables and set to empty values
$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 = $_POST["gender"];
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
Run example »
However, in the example above, all input fields are optional. The script works fine even if the user do not enter any data.
The next step is to make input fields required and create error messages if needed.

댓글
댓글 쓰기