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中获取关键词及所属来源搜索引擎名称的代码

复制代码 代码如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/s...

PHP的可变变量名的使用方法分享

通常变量通过下面这样的语句来命名 : 复制代码 代码如下: <?php $a = 'hello'; ?> 可变变量名指的是使用一个变量的值作为这个变量的名称。在上面的例子中...

PHP中改变图片的尺寸大小的代码

先介绍一个自己写的函数。 复制代码 代码如下: <?php $imgsrc = "/zb_users/upload/202003/vw0wu4pxkvk.png"; $width...

PHP的array_diff()函数在处理大数组时的效率问题

cisa 提交到 PHP 官方 BUG 页面上的方法 复制代码 代码如下: <?php /** * 解决 php 5.2.6 以上版本 array_diff() 函数在处理 * 大...

谷歌音乐搜索栏的提示功能php修正代码

谷歌音乐搜索栏的提示功能php修正代码

问题描述 在加载页面的时候, 将光标快速定位到搜索栏上, 待页面加载完成, 搜索栏进行初始化后会显示搜索提示. 此时输入的任何内容将成为搜索提示的一部分而不是搜索关键字. 截图如下:...