Sanitization Validation Programs
Sanitization Validation Programs
Example1
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?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;
}
?>
</body>
</html>
Example2
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $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["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
}
if (empty($_POST["comment"])) {
$comment = "";
} 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;
}
?>
</body>
</html>
Example3
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $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["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
}
if (empty($_POST["comment"])) {
$comment = "";
} 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;
}
?>
<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</body>
</html>
Example4
<?php
$name = $email = $address = $gender = "";
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$name = test($_POST["name"]);
$email = test($_POST["email"]);
$address = test($_POST["address"]);
$gender = test($_POST["gender"]);
}
function test($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<?php
echo "<h1>The data filled includes the following:</h1>";
echo $name. "<br>";
echo $email. "<br>";
echo $address. "<br>";
echo $gender. "<br>";
?>
Example5
<?php
$name = $email = $address = $gender = "";
$error_name = $error_email = $error_address = $error_gender = "";
if($_SERVER['REQUEST_METHOD']=='POST')
{
if(empty($_POST['name']))
{
$error_name="Name is mandatory field";
}
else
{
$name = test($_POST['name']);
}
if(empty($_POST['email']))
{
$error_email="Email is mandatory field";
}
else
{
$email = test($_POST['email']);
}
if(empty($_POST['address']))
{
$error_address="Address is mandatory field";
}
else
{
$address = test($_POST['address']);
}
if(empty($_POST['gender']))
{
$error_gender="Gender is mandatory field";
}
else
{
$gender = test($_POST['gender']);
}
function test($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<?php
echo "<h1>The data filled includes the following:</h1>";
echo $name. "<br>";
echo $email. "<br>";
echo $address. "<br>";
echo $gender. "<br>";
?>
Example6
<?php
$name = $email = $address = $gender = "";
$error_name = $error_email = $error_address = $error_gender = "";
if($_SERVER['REQUEST_METHOD']=='POST')
{
if(empty($_POST['name']))
{
$error_name="Name is mandatory field";
}
else
{
$name = test($_POST['name']);
}
if(empty($_POST['email']))
{
$error_email="Email is mandatory field";
}
else
{
$email = test($_POST['email']);
}
if(empty($_POST['address']))
{
$error_address="Address is mandatory field";
}
else
{
$address = test($_POST['address']);
}
if(empty($_POST['gender']))
{
$error_gender="Gender is mandatory field";
}
else
{
$gender = test($_POST['gender']);
}
}
function test($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<?php
echo "<h1>The data filled includes the following:</h1>";
echo $name. "<br>";
echo $email. "<br>";
echo $address. "<br>";
echo $gender. "<br>";
?>
Example7
<?php
$name = $email = $address = $gender = "";
$error_name = $error_email = $error_address = $error_gender = "";
if($_SERVER['REQUEST_METHOD']=='POST')
{
if(empty($_POST['name']))
{
$error_name="Name is mandatory field";
}
else
{
$name = test($_POST['name']);
}
if(empty($_POST['email']))
{
$error_email="Email is mandatory field";
}
else
{
$email = test($_POST['email']);
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false)
{
$error_email="";
}
else
{
$error_email="Not a valid email";
}
}
if(empty($_POST['address']))
{
$error_address="Address is mandatory field";
}
else
{
$address = test($_POST['address']);
}
if(empty($_POST['gender']))
{
$error_gender="Gender is mandatory field";
}
else
{
$gender = test($_POST['gender']);
}
}
function test($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<?php
echo "<h1>The data filled includes the following:</h1>";
echo $name. "<br>";
echo $email. "<br>";
echo $address. "<br>";
echo $gender. "<br>";
?>