紧急求助!!php网站被攻击。 显示信息时过滤HTML代码htmlspecialchars( $info );取得客户端IP$_SERVER['REMOTE_ADDR']; 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 htmlspecialchars函数是转义html代码而不是“过滤HTML代码”即将html代码变为明文 前面看到您写过:$_POST = array_map( "addslashes", $_POST );$_GET =array_map( "addslashes", $_GET);$_COOKIE = array_map( "addslashes", $_COOKIE );还有别人提出过:$var1=addslashes($var)stripslashes()加上现在的htmlspecialchars( $info );别人还给我发了一个函数:function cleanValue($val){ if ( $val == "" ) { return ""; } $val = str_replace( " ", " ", $val ); $val = str_replace( "&", "&",$val ); $val = str_replace( "<!--", "<!--",$val ); $val = str_replace( "-->", "-->",$val ); $val = preg_replace( "/<script/i","<script", $val); $val = str_replace( ">", ">",$val ); $val = str_replace( "<", "<",$val ); $val = str_replace( "\"", """,$val ); $val = preg_replace( "/\n/", "<br>",$val ); // Convert literal newlines $val = preg_replace( "/\\\$/", "$", $val ); $val = preg_replace( "/\r/", "", $val ); // Remove literal carriage returns $val = str_replace( "!","!", $val ); $val = str_replace( "'","'",$val ); // IMPORTANT: It helps to increase sql query safety. // Ensure unicode chars are OK $val = preg_replace("/&#([0-9]+);/s", "&#\\1;", $val ); // Swop user inputted backslashes $val = preg_replace( "/\\\(?!&#|\?#)/", "\", $val ); return $val;} 这几种方法都有什么功能呢?在写入数据库的时候和读出数据显示的时候都需要哪些对应的操作?用那种方法会最简单,不需要加太多的函数? addslashes 对特殊字符"、'、\进行转义,变成\"、\'、\\stripslashes 是 addslashes 的反函数$_POST = array_map( "addslashes", $_POST );将对$_POST的每一个元素都进行addslashes操作,这样就不必一一列出了htmlspecialchars 对html字符<、>、"、'进行转义,变成&lt;、&gt;、&quot;、&apos;他的反函数是html_entity_decode cleanValue为自定义函数,扩展了htmlspecialchars的作用范围,但实际意义不大 多谢!最后请教一下,如果需要防范前面的那种攻击,还有sql注入,需要用哪种方式呢?用了$_POST = array_map( "addslashes", $_POST );之后回头显示数据是不是都需要stripslashes一次? 多谢!最后请教一下,如果需要防范前面的那种攻击,还有sql注入,需要用哪种方式呢?都加上就没问题了! 那样的话是不是可以:输入数据库的时候用:$_POST = array_map( "addslashes", $_POST );$_POST = array_map( "htmlspecialchars", $_POST );从数据库中取出数据的时候用:$query = "select * from mydb";$result = mysql_query($query);$line = mysql_fetch_array($result, MYSQL_ASSOC);$line = array_map( "stripslashes", $line);$line = array_map( "html_entity_decode", $line );最后就可以正常输出了??还请指出,实在是进行测试好麻烦。 $_POST = array_map( "addslashes", $_POST );$_POST = array_map( "htmlspecialchars", $_POST );这样以后应该没有问题了 楼上的都是差不多了,配置方面注意一下,全局变量最好关闭(register_globals = off)。magic_quotes_gpc = 1一般只要用 addslashes 和 htmlspecialchars 处理一下。数字参数判断is_numeric,非数字限制一下长度,php会自动将'替换为''除非magic_quotes_gpc = 0 都很好!!如果用Smarty的话,可以使用escape:"html" 好了,多谢各位的支持。最后解决办法为:在数据库连接的文件db.inc.php中添加如下语句:$postnum = count($_POST);if ($postnum<>0) { $_POST = array_map( "addslashes", $_POST ); $_POST = array_map( "htmlspecialchars", $_POST );} html嵌套的php的值传递到JS 有没有办法把一个html文件中的文字全部取出来? mysql备份问题。我晕了。 这是怎么做的!! url的问题 怎样获取页面上指定的信息 [原创]一套完整的商城程序,提供下载,大家PP 怎么拆离数组? 大家帮我看下这个POST.PHP的问题 接触php也一年半了,想请教下过来人接下来应该在哪些方面在学些什么知识 求解决方法!急 请问一下, pg_connect函数是不是可以直接调用的???
即将html代码变为明文
$_POST = array_map( "addslashes", $_POST );
$_GET =array_map( "addslashes", $_GET);
$_COOKIE = array_map( "addslashes", $_COOKIE );还有别人提出过:
$var1=addslashes($var)
stripslashes()加上现在的
htmlspecialchars( $info );别人还给我发了一个函数:
function cleanValue($val)
{
if ( $val == "" )
{
return "";
}
$val = str_replace( " ", " ", $val );
$val = str_replace( "&", "&",$val );
$val = str_replace( "<!--", "<!--",$val );
$val = str_replace( "-->", "-->",$val );
$val = preg_replace( "/<script/i","<script", $val);
$val = str_replace( ">", ">",$val );
$val = str_replace( "<", "<",$val );
$val = str_replace( "\"", """,$val );
$val = preg_replace( "/\n/", "<br>",$val ); // Convert literal newlines
$val = preg_replace( "/\\\$/", "$", $val );
$val = preg_replace( "/\r/", "", $val ); // Remove literal carriage returns
$val = str_replace( "!","!", $val );
$val = str_replace( "'","'",$val ); // IMPORTANT: It helps to increase sql query safety.
// Ensure unicode chars are OK
$val = preg_replace("/&#([0-9]+);/s", "&#\\1;", $val );
// Swop user inputted backslashes
$val = preg_replace( "/\\\(?!&#|\?#)/", "\", $val );
return $val;
}
这几种方法都有什么功能呢?在写入数据库的时候和读出数据显示的时候都需要哪些对应的操作?用那种方法会最简单,不需要加太多的函数?
stripslashes 是 addslashes 的反函数$_POST = array_map( "addslashes", $_POST );
将对$_POST的每一个元素都进行addslashes操作,这样就不必一一列出了htmlspecialchars 对html字符<、>、"、'进行转义,变成&lt;、&gt;、&quot;、&apos;
他的反函数是html_entity_decode cleanValue为自定义函数,扩展了htmlspecialchars的作用范围,但实际意义不大
都加上就没问题了!
输入数据库的时候用:$_POST = array_map( "addslashes", $_POST );$_POST = array_map( "htmlspecialchars", $_POST );
从数据库中取出数据的时候用:
$query = "select * from mydb";
$result = mysql_query($query);
$line = mysql_fetch_array($result, MYSQL_ASSOC);
$line = array_map( "stripslashes", $line);$line = array_map( "html_entity_decode", $line );最后就可以正常输出了??还请指出,实在是进行测试好麻烦。
$_POST = array_map( "htmlspecialchars", $_POST );这样以后应该没有问题了
magic_quotes_gpc = 1
一般只要用 addslashes 和 htmlspecialchars 处理一下。
数字参数判断is_numeric,非数字限制一下长度,php会自动将'替换为''除非magic_quotes_gpc = 0
在数据库连接的文件db.inc.php中添加如下语句:$postnum = count($_POST);
if ($postnum<>0) {
$_POST = array_map( "addslashes", $_POST );
$_POST = array_map( "htmlspecialchars", $_POST );
}