优化dis
来源:互联网 发布:mac lamp 一键安装包 编辑:程序博客网 时间:2024/05/21 06:37
转自 小辉http://www.xiaohui.com
在这个函数中,有三个参数:
$pi_strName: 变量名
$pi_Def: 默认值
$pi_iType: 数据类型。取值为 XH_PARAM_INT, XH_PARAM_TXT, 分别表示数值型和文本型。
如果请求是数值型,那么调用 is_numeric() 判断是否为数值。如果不是,则返回程序指定的默认值。
简单起见,对于文本串,我将用户输入的所有危险字符(包括HTML代码),全部转义。由于 php 函数 addslashes()存在漏洞,我用 str_replace()直接替换。get_magic_quotes_gpc() 函数是 php 的函数,用来判断 magic_quotes_gpc 选项是否打开。
define("XH_PARAM_INT",0);define("XH_PARAM_TXT",1);function PAPI_GetSafeParam($pi_strName, $pi_Def = "", $pi_iType = XH_PARAM_TXT){ if ( isset($_GET[$pi_strName]) ) $t_Val = trim($_GET[$pi_strName]); else if ( isset($_POST[$pi_strName])) $t_Val = trim($_POST[$pi_strName]); else return $pi_Def; // INT if ( XH_PARAM_INT == $pi_iType) { if (is_numeric($t_Val)) return $t_Val; else return $pi_Def; } // String $t_Val = str_replace("&", "&",$t_Val); $t_Val = str_replace("<", "<",$t_Val); $t_Val = str_replace(">", ">",$t_Val); if ( get_magic_quotes_gpc() ) { $t_Val = str_replace("\\\"", """,$t_Val); $t_Val = str_replace("\\''", "'",$t_Val); } else { $t_Val = str_replace("\"", """,$t_Val); $t_Val = str_replace("'", "'",$t_Val); } return $t_Val;}
在这个函数中,有三个参数:
$pi_strName: 变量名
$pi_Def: 默认值
$pi_iType: 数据类型。取值为 XH_PARAM_INT, XH_PARAM_TXT, 分别表示数值型和文本型。
如果请求是数值型,那么调用 is_numeric() 判断是否为数值。如果不是,则返回程序指定的默认值。
简单起见,对于文本串,我将用户输入的所有危险字符(包括HTML代码),全部转义。由于 php 函数 addslashes()存在漏洞,我用 str_replace()直接替换。get_magic_quotes_gpc() 函数是 php 的函数,用来判断 magic_quotes_gpc 选项是否打开。