PHP中实现表单变量的安全处理,防止SQL注入

来源:互联网 发布:股票自动挂单软件 编辑:程序博客网 时间:2024/06/05 22:11
 

下面是通过PHP数组实现的一个处理办法

array_map()----------将回调函数作用到给定数组的单元上

get_magic_quotes_gpc()------Returns the current configuration setting of magic_quotes_gpc

addslashes()-------返回字符串,该字符串为了数据库查询语句等的需要在某些字符前加上了反斜线。这些字符是单引号(')、双引号(")、反斜线(\)与 NUL(NULL 字符)。

strip_tags($str)------This function tries to return a string with all HTML and PHP tags stripped from a given str.


<?php

function getSafeValue($value

     if (! 
get_magic_quotes_gpc()) { 
         return 
strip_tags(addslashes($value)); 
     } else { 
         return 
strip_tags($value); 
     } 

$safe_values array_map('getSafeValue'$_GET); 
?>

当然如果是通过POST方式传值,就将$_GET换成$_POST,或者用$_REQUEST代替。

原创粉丝点击