PHP strpos()返回奇数结果
我为我的webapp编写了一个基本的“安全检查程序”.我需要一目了然地看看用户提交的代码是否包含恶意内容.
这是我现在正在运行此代码的代码的屏幕截图:http://cl.ly/677a6dc40034f096697f 这是我用来对这三行代码的PHP代码: <!-- The View --> <h2>Security analysis</h2> <?php echo securitycheck($html,$css,$js); ?> – // The controller function securitycheck($html,$js) { // The code is the html,css,and js,appended together. We're scanning it all. $code = $html." ".$css." ".$js; // $insecure is our array of naughty things to search for. $insecure = array( /* HTML Elements */ 'applet','basefont','base','behavior','bgsound','blink','embed','expression','frameset','frame','ilayer','iframe','isindex','javascript','layer','link','meta','object','plaintext','style','script','xml','xss',/* Javascript Elements */ 'alert','cmd','passthru','eval','exec','system','fopen','fromcharcode','fsockopen','file','file_get_contents','readfile','unlink',/* Misc Elements */ 'vbscript:','<?','<?php','?>' ); $found = ""; $output = "<p><strong>Potentially insecure items found:</strong> "; foreach($insecure as $item) { if (($pos = strpos($code,$item)) !== FALSE) { $found .= "$item,"; } } if ($found == "") { $output .= "None.<br/>"; } else { $output .= "<span class="alert">".substr($found,-2)."</span>"."</p><br/>"; // cuts trailing comma and space from $found } return $output; } 最后,这是返回输出的截图(用HTML格式):http://cl.ly/f246dc419fb499dd6bd7 看截图?有几件事是错的.尾随空格和逗号没有被截断(我使用了substr()来做,并且它正在报告两个警报,正如您从第一个屏幕截图中看到的那样,只有一个已经通过这个. 我究竟做错了什么? 谢谢! 插口 编辑:正如Fosco所指出的,警报在我的阵列中被列出两次(doh!).我已经解决了这个问题,但是留下尾随逗号的问题仍然存在.我知道这是一个较小的问题,但我发誓它仍然不应该在那里…… 解决方法
一目了然,您的代码看起来应该提供您想要的输出.我不确定出了什么问题.
我建议将它构造为数组,然后使用implode()来获取字符串,而不是将$found作为字符串构建: > replace $found =“”; with $found = array();> replace $found.=“$item,”;使用$found [] = $item;并替换此代码块: if ($found == "") { $output .= "None.<br/>"; } else { $output .= "<span class="alert">".substr($found,-2)."</span>"."</p><br/>"; // cuts trailing comma and space from $found } 有了这个: if (!count($found)) { $output .= "None.<br/>"; } else { $output .= "<span class="alert">".implode(',',$found)."</span>"."</p><br/>"; // cuts trailing comma and space from $found } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |