<?php $pid=$_GET['pid']; //we create an object of a fictional class Page $obj=new Page; $content=$obj->fetchPage($pid); //and now we have a bunch of PHP that displays the page //...... //...... ?>
<?php $pid=$_GET['pid']; if (is_numeric($pid)){ //we create an object of a fictional class Page $obj=new Page; $content=$obj->fetchPage($pid); //and now we have a bunch of PHP that displays the page //...... //...... }else{ //didn't pass the is_numeric() test, do something else! }?>
这个方法似乎是有效的,但是以下这些输入都能够轻松地通过 is_numeric() 的检查:
100 (有效)
100.1 (不应该有小数位)
+0123.45e6 (科学计数法 —— 不好)
0xff33669f (十六进制 —— 危险!危险!)
那么,有安全意识的 PHP 开发人员应该怎么做呢?多年的经验表明,最好的做法是使用正则表达式来确保整个 GET 变量由数字组成,如下所示:
<?php $pid=$_GET['pid']; <b> if (strlen($pid)){ if (!ereg("^[0-9]+$",$pid)){ //do something appropriate, like maybe logging them out or sending them back to home page } }else{ //empty $pid, so send them back to the home page } </b> //we create an object of a fictional class Page, which is now //moderately protected from evil user input $obj=new Page; $content=$obj->fetchPage($pid); //and now we have a bunch of PHP that displays the page //...... //...... ?>
<?php class Page{ function fetchPage($pid){ $sql="select pid,title,desc,kw,content, status from page where pid=' ".mysql_real_escape_string($pid)."'"; //etc, etc.... } } ?>
<?php $pid=$_GET['pid']; if (strlen($pid)){ if (!ereg("^[0-9]+$",$pid) &&strlen($pid) >5){ //do something appropriate, like maybe logging them out or sending them back to home page } }else{ //empty $pid, so send them back to the home page } //we create an object of a fictional class Page, which is now //even more protected from evil user input $obj=new Page; $content=$obj->fetchPage($pid); //and now we have a bunch of PHP that displays the page //...... //...... ?>
现在,任何人都无法在数据库应用程序中塞进一个 5,000 位的数值 —— 至少在涉及 GET 字符串的地方不会有这种情况。想像一下黑客在试图突破您的应用程序而遭到挫折时咬牙切齿的样子吧!而且因为关闭了错误报告,黑客更难进行侦察。