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实现下载功能的代码

wzskynet#163.com ·php escapeshellcmd多字节编码漏洞 ·详细讲解PHP中缓存技术的应用 ·利用PHP V5开发多任务应用程序 ·详细解析 PHP 向 M...

PHP下载远程图片的几种方法总结

PHP下载远程图片的几种方法总结 本文演示3个从远程URL下载图片,并保存到本地文件中的方法,包括file_get_contents,curl和fopen。 1. 使用file_get...

PHP实现对数组分页处理实例详解

本文实例讲述了PHP实现对数组分页处理方法。分享给大家供大家参考,具体如下: 最近用到了用数组数据分页,这里整理了一下,具体代码如下: <?php class Pagi...

PHP数组操作类实例

本文实例讲述了PHP数组操作类。分享给大家供大家参考。具体如下: class ArrayHelper{ /** * 从数组中删除空白的元素(包括只有空白字符的元素)...

深入研究PHP中的preg_replace和代码执行

深入研究PHP中的preg_replace和代码执行

前言 本文将深入研究 preg_replace /e 模式下的代码执行问题,其中包括 preg_replace 函数的执行过程分析、正则表达式分析、漏洞触发分析,当中的坑非常多,相信看完...