PHP Validation

PHP validation is used to check whether the field is filled or not in the proper way by the user.

There are two types of validation in PHP.

  • Client Side Validation:  Client side validation is performed on the client machine web browsers.
  • Server Side Validation: Server side validation is performed after submitting the data by user to check the validation on the server machine.

Following are the some validation rules for field

Field Validation Rules
Name Name should required letters and white-spaces
Website Website should required a valid URL.
Radio Radio must be selectable at least once.
Check Box Checkbox must be checkable at least once.
Email Email should required @ and.

Let us take an example of Form validation with require.

Example

.error {color: #FF0000;}

.divid{background-color:#9e9e9eb0;height: 290px;width: 170px;padding: 50px;margin:auto;}

// All the defined variables  set to empty values

$nameErr = $emailErr= $commentErr = $genderErr = $websiteErr = “”;

$name = $email = $gender = $comment = $website = “”;

if ($_SERVER[“REQUEST_METHOD”] == “POST”) {

if (empty($_POST[“name”])) {

$nameErr= “Name is required”;

} else {

$name = test_input($_POST[“name”]);

}

if (empty($_POST[“email”])) {

$emailErr = “Email is required”;

} else {

$email = test_input($_POST[“email”]);

}

if (empty($_POST[“comment”])) {

$commentErr = “comment is required”;

} else {

$comment = test_input($_POST[“comment”]);

}

if (empty($_POST[“gender”])) {

$genderErr = “Gender is required”;

} else {

$gender = test_input($_POST[“gender”]);

}

}

function test_input($data) {

$data = trim($data);

$data = stripslashes($data);

$data = htmlspecialchars($data);

return $data;

}

?>

Note:

The “$_SERVER[“PHP_SELF”]” variable  is a super global variable that  is used to

returns the filename of the current executing script. It is always used by hackers.

The “htmlspecialchars()”  function  is used to convert special characters to HTML entities( like: < and > with &lt; and &gt;).

Example

Output

PHP Captcha 

PHP CAPTCHA stands for Competently Automated Public Turing  test to tell Computers and Humans Apart. It is a type of challenge –response test that is used to determine whether user is human or not.

Example of Arithmetic CAPTCHA

.divid{background-color:#9e9e9eb0;height: 100px;width: 200px;padding: 50px;margin:auto;}

Output

PHP  Mail

PHP mail() function is  used to send the mail  in PHP with various format like test message, html message and attachment message or file.

Syntax:

Parameter Description
to The recipient’s email address.
subject Subject of the email to be sent. This parameter cannot contain any newline characters(/n).
message It defines the message to be sent. Each line should be separated with a LF (\n). Lines should not exceed 70 characters
headers It is optional and specify additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n)
parameters It is used to pass additional parameter.

Sending Plain Text Emails

The easy way to send an email with PHP, only we have to pass three parameter in mail() method.

Let us take an example

Output

Your mail has been sent successfully

PHP Mail with Attachment

We can also send mail with attachment only we have to include header information. Let us consider an example.

Leave a Comment