php 对输入信息的进行安全过滤的函数代码

yipeiwu_com6年前PHP代码库
复制代码 代码如下:

// define constannts for input reading
define('INPUT_GET', 0x0101);
define('INPUT_POST', 0x0102);
define('INPUT_GPC', 0x0103);

/**
* Read input value and convert it for internal use
* Performs stripslashes() and charset conversion if necessary
*
* @param string Field name to read
* @param int Source to get value from (GPC)
* @param boolean Allow HTML tags in field value
* @param string Charset to convert into
* @return string Field value or NULL if not available
*/
function get_input_value($fname, $source, $allow_html=FALSE, $charset=NULL) {
$value = NULL;

if ($source == INPUT_GET && isset($_GET[$fname]))
$value = $_GET[$fname];
else if ($source == INPUT_POST && isset($_POST[$fname]))
$value = $_POST[$fname];
else if ($source == INPUT_GPC) {
if (isset($_POST[$fname]))
$value = $_POST[$fname];
else if (isset($_GET[$fname]))
$value = $_GET[$fname];
else if (isset($_COOKIE[$fname]))
$value = $_COOKIE[$fname];
}

if (empty($value))
return $value;

// strip single quotes if magic_quotes_sybase is enabled
if (ini_get('magic_quotes_sybase'))
$value = str_replace("''", "'", $value);
// strip slashes if magic_quotes enabled
else if (get_magic_quotes_gpc() || get_magic_quotes_runtime())
$value = stripslashes($value);

// remove HTML tags if not allowed
if (!$allow_html)
$value = strip_tags($value);

// convert to internal charset
return $value;
}

用法:get_input_value('_uid', INPUT_GET)

相关文章

PHP工程师VIM配置分享

PHP工程师VIM配置分享

关于我的vim配置 经常在服务器上写代码,一个朋友提供了关于他的vim配置,在此分享给大家。 界面效果图: .vimrc 内容如下  "==================...

php中ob_get_length缓冲与获取缓冲长度实例

本文实例讲述了php中ob_get_length缓冲与获取缓冲长度的方法。分享给大家供大家参考。具体方法如下: file_get_contents() 函数把整个文件读入一个字符串中,和...

浅析Apache中RewriteCond规则参数的详细介绍

RewriteCond就像我们程序中的if语句一样,表示如果符合某个或某几个条件则执行RewriteCond下面紧邻的RewriteRule语句,这就是RewriteCond最原始、基础...

curl 出现错误的调试方法(必看)

实例如下: private function httpGet($url) { $curl = curl_init(); curl_setopt($curl,...

学习php设计模式 php实现状态模式

学习php设计模式 php实现状态模式

一、意图 允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它的类 状态模式变化的位置在于对象的状态 二、状态模式结构图   三、状态模式中主要角色 抽象状态...