Академический Документы
Профессиональный Документы
Культура Документы
November 6 by Clay
Recently i have involve myself in another application development. Regular
Hungred Dot Com visitors will notice that the site currently offer advertisement
space through this form. But really, we as a developers are always looking for
such snippets or writing them out from scratch every single time regardless of
how many time we know we have store it somewhere in our laptop! Man, its
really frustrating searching on Google and find all sort of solution and trying to
figure out whether the regular expression implemented is expensive or complete.
So i came out with an idea to ease my life a bit and other developers by putting
up an article such as this for my/our references. (This can be made into a class if
you like to)
Validate Email
This is not possible but certain validation can be use to validate email existence.
function check_email($email)
{
$email_error = false;
$Email = htmlspecialchars(stripslashes(strip_tags(trim($email)))); //parse
unnecessary characters to prevent exploits
if ($Email == '') { email_error = true; }
elseif (!eregi('^([a-zA-Z0-9._-])+@([a-zA-Z0-9._-])+\.([a-zA-Z0-9._-])([a-zA-Z09._-])+', $Email)) { email_error = true; }
else {
list($Email, $domain) = split('@', $Email, 2);
if (! checkdnsrr($domain, 'MX')) { email_error = true; }
else {
$array = array($Email, $domain);
$Email = implode('@', $array);
}
}
We can use PHP built-in function to validate whether a given value is a number.
function fnValidateNumber($value)
{
#is_ double($value);
#is_ float($value);
#is_ int($value);
#is_ integer($value);
return is_numeric($value);
}
PHP 5.2 and above.
function fnValidateNumber($value)
{
#return filter_var($value, FILTER_VALIDATE_FLOAT); // float
return filter_var($value, FILTER_VALIDATE_INT); # int
}
Sanitize Number
function fnSanitizeNumber($value)
{
#return filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT); // float
return filter_var($value, FILTER_SANITIZE_NUMBER_INT); # int
}
Validate String Only
Sometimes to validate name we can use this function to restrict only letters and
spaces.
function fnValidateStringr($str)
{
#letters and space only
return preg_match('/^[A-Za-z\s ]+$/', $str);
}
Sanitize String
This function will check whether a given URL exist and not only validate it.
function url_exist($url)
{
$url = @parse_url($url);
if (!$url)
{
return false;
}
if ($path == '')
{
$path = '/';
}
if (!$fp)
{
return false;
}
fputs($fp, 'HEAD $path HTTP/1.1\r\nHost: $url[host]\r\n\r\n');
$headers = fread($fp, 4096);
fclose($fp);
}
$headers = (is_array($headers)) ? implode('\n', $headers) : $headers;
This function will validate a given url to ensure the format is correct.
function fnValidateUrl($url){
return preg_match('/^(http(s?):\/\/|ftp:\/\/{1})((\w+\.){1,})\w{2,}$/i', $url);
}
PHP 5.2 and above.
function fnValidateUrl($url)
{
return filter_var($url, FILTER_VALIDATE_URL);
}
Sanitize URL
This function will check whether a given image link exist and not only validate it.
function image_exist($url) {
if(@file_get_contents($url,0,NULL,0,1)){return 1;}else{ return 0;}
}
Validate IP Address
This function will let us detect proxy visitors even those that are behind
anonymous proxy.
function fnValidateProxy(){
if ($_SERVER['HTTP_X_FORWARDED_FOR']
|| $_SERVER['HTTP_X_FORWARDED']
|| $_SERVER['HTTP_FORWARDED_FOR']
|| $_SERVER['HTTP_VIA']
|| in_array($_SERVER['REMOTE_PORT'],
array(8080,80,6588,8000,3128,553,554))
|| @fsockopen($_SERVER['REMOTE_ADDR'], 80, $errno, $errstr, 30))
{
exit('Proxy detected');
}
}
Validate Username
return preg_match('/^([0-9]{5})(-[0-9]{4})?$/i',$postalcode);
}
Validate US Social Security Numbers
function fnValidateUSSocialSecurityCode($ssb){
#eg. 531-63-5334
return preg_match('/^[\d]{3}-[\d]{2}-[\d]{4}$/',$ssn);
}
Validate Credit Card
}
This is a date format YYYY-DD-MM or YY-MM-DD validation which validate from
year 0000-9999.
function fnValidateDate($date){
#2009/12/11
#2009-12-11
#2009.12.11
#09.12.11
return preg_match('#^([0-9]?[0-9]?[0-9]{2}[- /.](0?[1-9]|1[012])[- /.](0?[1-9]|
[12][0-9]|3[01]))*$#'', $date);
}
Validate Hexadecimal Colors
This is a good validation for people who allows their user to change color in their
system.
function fnValidateColor($color){
#CCC
#CCCCC
#FFFFF
return preg_match('/^#(?:(?:[a-f0-9]{3}){1,2})$/i', $color);
}
Make Query Safe
function _clean($str){
return is_array($str) ? array_map('_clean', $str) : str_replace('\\', '\\\\',
htmlspecialchars((get_magic_quotes_gpc() ? stripslashes($str) : $str),
ENT_QUOTES));
This function help to keep us protected against XSS, JS and SQL injection by
removing tags.
function _clean($str){
return is_array($str) ? array_map('_clean', $str) : str_replace('\\', '\\\\',
strip_tags(trim(htmlspecialchars((get_magic_quotes_gpc() ? stripslashes($str) :
$str), ENT_QUOTES))));
}