Validation File

Encrypted Messenger PHP

empty

Determine whether a variable is considered to be empty.
A variable is considered empty if it does not exist or if its value equals FALSE.
empty() does not generate a warning if the variable does not exist.

filter_var

The filter_var() function filters a variable with the specified filter.

stripslashes

The stripslashes() function removes backslashes added by the addslashes() function.
Tip : This function can be used to clean up data retrieved from a database or from an HTML form.

strlen

Returns the length of the given string.

strtolower

The strtolower() function converts a string to lowercase.
Note: This function is binary-safe.
Related functions:
strtoupper() – converts a string to uppercase
lcfirst() – converts the first character of a string to lowercase
ucfirst() – converts the first character of a string to uppercase
ucwords() – converts the first character of each word in a string to uppercase

ctype_xdigit

Checks if all of the characters in the provided string, text, are hexadecimal ‘digits’.

Create a file named secured-validation.php


<?php

//***********************************************************************************************

$gr=array();

//***********************************************************************************************

function gns_validate($n,$t)
{
global $gr;

$a=$_POST[$n];

//**************************

if( empty($a) )
{
$gr[$n]="Empty";
return false;
}

//**************************

$x=$a;

if($t=="user" || $t=="email" || $t=="pass")
{
$x = trim($x);
$x = filter_var($x, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$x = filter_var($x, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
$x = filter_var($x, FILTER_SANITIZE_SPECIAL_CHARS , FILTER_FLAG_STRIP_BACKTICK );
}

$x = stripslashes($x);
$x = filter_var($x, FILTER_SANITIZE_FULL_SPECIAL_CHARS);

if($a!=$x)
{
$gr[$n]="Security Reason";
return false;
}

//**************************

if($t=="user")
{

if( strlen($a)<4 )
{
$gr[$n]="Username Length Less Than 4";
return false;
}

if( strlen($a)>49 )
{
$gr[$n]="Username Length More Than 49";
return false;
}

if( strtolower($a)!=$a )
{
$gr[$n]="Username Should Be Lowercase";
return false;
}

}

//**************************

if( $t=="pass")
{

if( strlen($a)<8 )
{
$gr[$n]="Password Length Less Than 8";
return false;
}

if( strlen($a)>49 )
{
$gr[$n]="Password Length More Than 49";
return false;
}

}

//**************************

if($t=="sub")
{

if( strlen($a)<3 )
{
$gr[$n]="Subject Length Less Than 3";
return false;
}

if( strlen($a)>49 )
{
$gr[$n]="Subject Length More Than 49";
return false;
}

}

//**************************

if($t=="txt")
{

if( strlen($a)<3 )
{
$gr[$n]="Message Length Less Than 3";
return false;
}

if( strlen($a)>343 )
{
$gr[$n]="Message Length More Than 343";
return false;
}

}

//**************************

if($t=="email")
{

$b = filter_var($a, FILTER_SANITIZE_EMAIL);
if($a!=$b)
{
$gr[$n]="Security Reason";
return false;
}

if( ! filter_var($a, FILTER_VALIDATE_EMAIL) )
{
$gr[$n]="Invalid Email";
return false;
}

if( strlen($a)<7 )
{
$gr[$n]="Email Length Less Than 7";
return false;
}

if( strlen($a)>49 )
{
$gr[$n]="Email Length More Than 49";
return false;
}

}

//**************************

return true;
}

//***********************************************************************************************

function gns_show_error($n)
{
global $gr;

if( isset($_POST[$n]) && isset($gr[$n]) )
{
echo $gr[$n];
return true;
}

return false;
}

//***********************************************************************************************

function gns_show_value($n)
{
global $gr;

if( isset($_POST[$n]) && !isset($gr[$n]) )
echo $_POST[$n];
}

//***********************************************************************************************

function gns_check_form($x)
{
$r=true;

foreach($x as $a=>$b)
if( ! gns_validate($a,$b) )
$r=false;

return $r;
}

//***********************************************************************************************
function gns_validate_cookie($a)
{
if(empty($a))
return false;

if(!strlen($a)==128)
return false;

if(!ctype_xdigit($a))
return false;

$x=$a;
$x=trim($x);
$x = stripslashes($x);
$x = filter_var($x, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$x = filter_var($x, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
$x = filter_var($x, FILTER_SANITIZE_SPECIAL_CHARS , FILTER_FLAG_STRIP_BACKTICK );
$x = filter_var($x, FILTER_SANITIZE_FULL_SPECIAL_CHARS);

if($a!=$x)
return false;

return true;
}

//***********************************************************************************************

?>

Full structure is available at Encrypted Messenger PHP