Monday, December 30, 2013

Common security functions in PHP

12/30/2013





<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

/**
 * IsEmpty
 * 
 * @param mixed $value
 * @return boolean
 */
function IsEmpty($value) {
    if(strlen(trim(preg_replace('/\xc2\xa0/',' ',$value))) == 0) {
        return true;
    }else {
        return false;
    }
}


?>




<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

/**
 * Sanitize
 * 
 * @param mixed $value
 * @return string
 */
function Sanitize($value) {
    if(get_magic_quotes_gpc()) $value = stripslashes($value);
    return htmlentities($value, ENT_QUOTES, 'utf-8');
}


?>





<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

/**
 * ParseId
 * 
 * @param integer $id
 * @param integer $segment
 * @return mixed
 */
function ParseId($id, $segment = 2) {
    $pattern = '/^([0-9]+)-';
    for($i = 2; $i < $segment; $i++) {
        $pattern .= '([0-9]+)-';
    }
    $pattern .= '([0-9]+)$/';
    if(!preg_match($pattern, $id, $match)) {
        return false;
    }else {
        return $match;
    }
}


?>
<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


/**
 * ValidEmail
 * 
 * @param string $email
 * @return boolean
 */
function ValidEmail($email) {
    if(preg_match("/^[0-9a-z]+(([\.\-_])[0-9a-z]+)*@[0-9a-z]+(([\.\-])[0-9a-z-]+)*\.[a-z]{2,4}$/i", $email)) {
        return true;
    }else {
        return false;
    }
}

?>
<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


/**
 * ValidURL
 * 
 * @param string $email
 * @return boolean
 */
function ValidURL($url) {
    if(preg_match('/^https?:\/\/[^\s]+/i', $url)) {
        return true;
    }else {
        return false;
    }
}

?>
<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


/**
 * ValidString
 * 
 * @param string $string
 * @return boolean
 */
function ValidString($string) {
    if(preg_match('/^[a-z0-9A-z ]*$/i', $string)) {
        return true;
    }else {
        return false;
    }
}


?>
<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

/**
 * ValidUserName
 * 
 * @param string $string
 * @return boolean
 */
function ValidUserName($string) {
    if(preg_match('/^[a-z0-9]*$/i', $string)) {
        return true;
    }else {
        return false;
    }
}

?>
<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

/**
 * ValidImg
 * 
 * @param string $file
 * @return boolean
 */
function ValidImg($file) {
    $extensions = array('jpg','gif','png');
    $len  = strLen($file)-3;
    $ext  = strtolower(substr($file, $len, 3));
    if(in_array($ext, $extensions)) {
        return true;
    }else {
        return false;
    }
}


?>

helpful? Share this

The Editorial Team of 4everTutorials consists of a group of PHP Professionals.

0 comments:

 

© 2014 4everTutorials. All rights resevered.

Back To Top