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

yipeiwu_com5年前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将会员数据导入到ucenter的代码

我们要用的会员表结构 复制代码 代码如下: create table if not exists `net_111cnnet` ( `id` int(11) not null auto_...

php以fastCGI的方式运行时文件系统权限问题及解决方法

php以fastCGI的方式运行时文件系统权限问题及解决方法

今天准备将一个php demo放在IIS下运行,网站在IIS下的配置是这样的: 应用程序池是集成模式下的.net framework 2.0(2.0或4.0没什么关系,因为php以fas...

PHP URL地址获取函数代码(端口等) 推荐

php 获得当前的脚本网址(只有路径) 复制代码 代码如下: function GetCurUrl() { if(!empty($_SERVER["REQUEST_URI"])) { $...

php类自动装载、链式操作、魔术方法实现代码

1、自动装载实例 目录下有3个文件:index.php load.php tests文件夹 tests文件夹里有 test1.php <?php namespace T...

php登录超时检测功能实例详解

php登录超时检测功能实例详解 前言: php登录超时问题,当用户超过一定时间没有操作页面时自动退出登录,原理是通过js进行访问判断的!代码如下(以thinkphp5.0版本为例) 1、...