Zend - The PHP Company




Forms

Add Code


Check parameters validity  

Type: class
Added by: eovidiu
Entered: 03/11/2000
Last modified: 01/12/2000
Rating: - (fewer than 3 votes)
Views: 11669
Paranoia was designed to check the validity of the parameters that a php page will receive after a form submision. It can be used to check the variables sent by POST or GET


<?php
/****************************************************************
*Author: Ovidiu EFTIMIE
*Copyright:Ovidiu EFTIMIE
*Last modified:Friday, November 03, 2000 10:43:17 AM
*Description : Checks an array to see if it contains invalid chars
*****************************************************************/
class paranoia{
var 
$wrongParams=false;
var 
$para=array("~","`","!","@","#","$","%","^","&","*","(",")","_","-","+","=","|",
                
"\","{","}",":",";",""","'",",","<",".",">","?","/");


/**************************************************
*Class constructor
*Params : $paramsArray - the array containing the data to be checked
***************************************************/
function paranoia($paramsArray){
reset($this->para);
$this->wrongParams=false;
while(list(
$k,$postvars)=each($paramsArray)){
    if(
is_array($postvars)){
        while(list(
$r,$postvals)=each($postvars)){
            while(list(,
$val)=each($this->para)){
                
$wrong=strchr($postvals,$val);
                if(!empty(
$wrong)){
                    
$this->wrongParams=true;
                }
            }
        }
    }else{
        while(list(,
$val)=each($this->para)){
            
$wrong=strchr($postvars,$val);
                if(!empty(
$wrong)){
                
$this->wrongParams=true;
                }
        }
        
reset($this->para);
    }
}
return 
$this->wrongParams;
}
/*******************************************************
*Checks to see the invalid chars
********************************************************/
function checkParanoia(){
echo 
date("d M Y H:i:s",time())."<br>Checking paranoia chars...<br><br>";
    while(list(
$d,$r)=each($this->para)){
        echo 
$d.".&nbsp;&nbsp; <b>".$r."</b>  =  <b>".ord($r)."</b><br>";
    }

}
/********************************************************
*Redirecting function
********************************************************/
function sendBack(){
echo 
'<html><head><title></title></head><body onLoad="javascript:history.back(-1);"></body></html>';
}
}





?>


Usage Example


<?php
require_once("paranoia.php");
if(isset(
$HTTP_POST_VARS)&&!empty($HTTP_POST_VARS)){
$par=new paranoia($HTTP_POST_VARS);
if(!empty(
$par->wrongParams)){
//    $par->sendBack();
    
echo "Incorrect parameters";
}else{
    echo 
"Corect parameters";
}
}
?>
<html>
<head><title>Test page for paranoia</title></head>
<form action="test_paranoia.php" method="POST">
    <input type="text" name="test">
    <input type="submit" value="Test">
</form>
</html>


Rate This Script





Search



This Category All Categories