PHP 5 Form Validation | php w3schools

This and the next chapters you will see how to use PHP to validate form data.

PHP Form Validation

Think SECURITY when processing PHP forms!
These pages will show you how to process PHP forms with security. Proper validation of a form data is important to protect your form from hackers and spammers!
The HTML form we will be working at in these chapters that contain various input fields: required and optional text fields, radio buttons, and a submit button:


PHP Form Validation Example

* required field
Name:  

E-mail:  

Website:  

Comment: 

Gender: Female Male Other 

Your Input:




The validations rules for the form above are as follow:
FieldValidation Rules
NameRequired. + Must only contain letters and whitespace
E-mailRequired. + Must contain  valid email address (with @ and .)
WebsiteOptional. If present, it must be contain a valid URL
CommentOptional. Multi-line input field (textarea)
GenderRequired. Must select one
First, we will look at the plain HTML code for the forms:

Text Fields

The name, email, and website fields are text input element, and the comment field is a textarea. The HTML code looks like this:
Name: <input type="text" name="name">
E-mail: <input type="text" name="email">
Website: <input type="text" name="website">
Comment: <textarea name="comment" rows="5" cols="40"></textarea>

Radio Buttons

The gender fields are radio buttons and the HTML code are looks like this:
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="other">Other

The Form Element

The HTML code of the forms looks like this:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
When the form is submitted, then the form data is sent with method="post".
What is the $_SERVER["PHP_SELF"] variable?

The $_SERVER["PHP_SELF"] is a super global variable that return  the filename of the currently executing script.
So, the $_SERVER["PHP_SELF"] sends the submitted the form data to the page itself, instead of jumping to a different page. This way, user will get error messages on the same page as the form.
What is the htmlspecialchars() function?

The htmlspecialchars() function converts special character  to HTML entities. This means that it will replace HTML characters like this < and > with &lt; and &gt;. This prevents attackers from exploiting the code by injecting HTML or Javascript code (Cross-site Scripting attack) in forms.

Big Note on PHP Form Security

The $_SERVER["PHP_SELF"] variable can be used by hackers!
If PHP_SELF is used in your page then a user can enter a slash (/) and then some Cross Site Scripting (XSS) commands be to execute.
The Cross-site script (XSS) is a type of computer security vulnerability typically found in Web applications.The XSS enables attackers to inject client-side script into the Web pages viewed by other users.
Assume we have a following form in a page named "test_form.php":
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
Now, if any user enters the normal URL in the address bar like "http://www.example.com/test_form.php", the above code will be translated to:
<form method="post" action="test_form.php">
So far, so good.
However, consider that a user enters the following URL in address bar:
http://www.example.com/test_form.php/%22%3E%3Cscript%3Ealert('hacked')%3C/script%3E
In this case, the above code will be translated into:
<form method="post" action="test_form.php/"><script>alert('hacked')</script>
This code adds with a script tag and an alert command. When a page load any JavaScript code will be executed (the user will see an alert box). This is just a simple and harmless example of how the PHP_SELF variable can be exploited.
Be aware of that any JavaScript code can be added inside the <script> tags! A hacker can redirect the user to a file on another server, and that file can hold malicious code that can alter the global variables or submit the form to another address to save the users data, for example.

How To Avoid $_SERVER["PHP_SELF"] Exploits?

$_SERVER["PHP_SELF"] exploits can be avoided by using the htmlspecialchars() function.
The form code should be look like this:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
htmlspecialchars() function converts special characters to HTML entities. Now if the user tries to exploit the PHP_SELF variable, it will result in the following output:
<form method="post" action="test_form.php/&quot;&gt;&lt;script&gt;alert('hacked')&lt;/script&gt;">
The exploit attempt fails, and no harm is to done!

Validate Form Data With PHP

The first thing we will do is to pass all variables through PHP's htmlspecialchars() functions.
When we use   htmlspecialchars() function; then if  user tries to submit the following in a text field:
<script>location.href('http://www.hacked.com')</script>
- it would not executed, because it would be saved as HTML escaped code, like this:
&lt;script&gt;location.href('http://www.hacked.com')&lt;/script&gt;
Code is now safe to be displayed on a page or inside a  e-mail.
We will also do two more things when the users submit  the form:
  1. Strip unnecessary character (extra space, tab, newline) from the user input data (with the PHP trim() function)
  2. Remove backslash (\) from the user input data (with the PHP stripslashes() function)
The next step is to create a function that will do all the checking for us (which is much more convenient than writing the same code over and over it again).
We will name the function test_input().
Now, we can check each $_POST variable with the test_input() function, and the script looks 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 = test_input($_POST["gender"]);
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>
Run example »
Notice that at the start of the scripts, we check whether the form has been submitted using $_SERVER["REQUEST_METHOD"]. If REQUEST_METHOD is POST, then the form has been submitted - and it should be validated. If it has not been submitted, skip the validations and display a blank form.
However, in the examples above, all input fields are optional. The script works fine even if the users does not enter any data.
The next steps is to make input fields required and create error messages if needed.

No comments:

Post a Comment