note 111349 added to reserved.variables.post
| From: | Anonymous | Date: | Sun, 10 Feb 2013 11:14:37 +0000 |
| Subject: | note 111349 added to reserved.variables.post | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-193570@lists.php.net to get a copy of this message | ||
/*
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;
}
----
Server IP: 69.147.83.199
Probable Submitter: 98.176.50.73
----
Manual Page -- http://php.net/manual/en/reserved.variables.post.php
Edit -- https://master.php.net/note/edit/111349
Del: integrated -- https://master.php.net/note/delete/111349/integrated
Del: useless -- https://master.php.net/note/delete/111349/useless
Del: bad code -- https://master.php.net/note/delete/111349/bad+code
Del: spam -- https://master.php.net/note/delete/111349/spam
Del: non-english -- https://master.php.net/note/delete/111349/non-english
Del: in docs -- https://master.php.net/note/delete/111349/in+docs
Del: other reasons-- https://master.php.net/note/delete/111349
Reject -- https://master.php.net/note/reject/111349
Search -- https://master.php.net/manage/user-notes.php