note 111349 modified in reserved.variables.post by danbrown

From: Date: Tue, 05 Mar 2013 16:08:31 +0000
Subject: note 111349 modified in reserved.variables.post by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-193918@lists.php.net to get a copy of this message
This is a handy function that can be used for validating user input <?php /* * @var $func is used like a callback function * it will return whatever value you make it return * * post( "$param" ) * post( "$param" , 55 ), * post( "$param" , 55 , "/[^a-z]/i" ) * post( "$param" , 55 , "/[^a-z]/i" , FILTER_SANATIZE_SPECIAL_CHARS) * post( "$param" ,55 , "/[^a-z0-9.]/i" , FILTER_SANATIZE_SPECIAL_CHARS, function($str){ if($str === 'red' ){ return false; }else { return true; } }) * post( "$param" , '/[^a-z]/i') * post( "$param",function(){} ) * * even more combinations workd. but to be SAFE skipping any value with NULL will alway work * * post("$param" , NULL , NULL , NULL , function(){}); * post("$param" , NULL , NULL , '/[^a-z]/i'); * post("$param" , 55 , NULL , '/[^a-z]/i'); * * even more will work. * * get a post value from global $_POST */ function post($param , $max_len = 256 , $filter = FILTER_SANITIZE_FULL_SPECIAL_CHARS, $replace = NULL , $func = NULL) { if($filter === NULL) # if filter is skipped using NULL { $filter = FILTER_SANITIZE_FULL_SPECIAL_CHARS; //set it to its default }elseif(is_string($filter)) //else if its a string { $replace = $filter; //set the replace to the string $filter = FILTER_SANITIZE_FULL_SPECIAL_CHARS; //and this back to its default }elseif (!is_integer($filter)) { $func = $filter; $filter = FILTER_SANITIZE_FULL_SPECIAL_CHARS; } if($max_len === NULL)//if max length was skipped { $max_len = 256; //set to default } if(!is_int($max_len)) { if(is_string($max_len)) { $replace = $max_len; }elseif(is_callable($max_len)) { $func = $max_len; } $max_len = 256; } $sub_input = ''; if ( isset($_POST[$param]) && !empty($_POST[$param])) # if the post paramter is set { $user_input = filter_input(INPUT_POST, $param, $filter); if ( $max_len > strlen($user_input) ) # if max length is greater then the len of the string { $max_len = strlen($user_input); # we use the max length of the string } $sub_input = substr($user_input, 0 , $max_len); }else { return $sub_input; } $return = preg_quote($sub_input); # take care of escape characters if($replace !== NULL) { $return = preg_replace($replace, '' , $return); } if ($func !== NULL) { return $func($return); } return $return; } ?> --was-- /* This is a handy function that can be used for validating user input */ /* * @var $func is used like a callback function * it will return whatever value you make it return * * post( "$param" ) * post( "$param" , 55 ), * post( "$param" , 55 , "/[^a-z]/i" ) * post( "$param" , 55 , "/[^a-z]/i" , FILTER_SANATIZE_SPECIAL_CHARS) * post( "$param" ,55 , "/[^a-z0-9.]/i" , FILTER_SANATIZE_SPECIAL_CHARS, function($str){ if($str === 'red' ){ return false; }else { return true; } }) * post( "$param" , '/[^a-z]/i') * post( "$param",function(){} ) * * even more combinations workd. but to be SAFE skipping any value with NULL will alway work * * post("$param" , NULL , NULL , NULL , function(){}); * post("$param" , NULL , NULL , '/[^a-z]/i'); * post("$param" , 55 , NULL , '/[^a-z]/i'); * * even more will work. * * get a post value from global $_POST */ function post($param , $max_len = 256 , $filter = FILTER_SANITIZE_FULL_SPECIAL_CHARS, $replace = NULL , $func = NULL) { if($filter === NULL) # if filter is skipped using NULL { $filter = FILTER_SANITIZE_FULL_SPECIAL_CHARS; //set it to its default }elseif(is_string($filter)) //else if its a string { $replace = $filter; //set the replace to the string $filter = FILTER_SANITIZE_FULL_SPECIAL_CHARS; //and this back to its default }elseif (!is_integer($filter)) { $func = $filter; $filter = FILTER_SANITIZE_FULL_SPECIAL_CHARS; } if($max_len === NULL)//if max length was skipped { $max_len = 256; //set to default } if(!is_int($max_len)) { if(is_string($max_len)) { $replace = $max_len; }elseif(is_callable($max_len)) { $func = $max_len; } $max_len = 256; } $sub_input = ''; if ( isset($_POST[$param]) && !empty($_POST[$param])) # if the post paramter is set { $user_input = filter_input(INPUT_POST, $param, $filter); if ( $max_len > strlen($user_input) ) # if max length is greater then the len of the string { $max_len = strlen($user_input); # we use the max length of the string } $sub_input = substr($user_input, 0 , $max_len); }else { return $sub_input; } $return = preg_quote($sub_input); # take care of escape characters if($replace !== NULL) { $return = preg_replace($replace, '' , $return); } if ($func !== NULL) { return $func($return); } return $return; } http://php.net/manual/en/reserved.variables.post.php

« previous php.notes (#193918) next »