首页 / PHP / PHP strpos()返回奇数结果
PHP strpos()返回奇数结果
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP strpos()返回奇数结果,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2418字,纯文字阅读大概需要4分钟。
内容图文
我为我的webapp编写了一个基本的“安全检查程序”.我需要一目了然地看看用户提交的代码是否包含恶意内容.
这是我现在正在运行此代码的代码的屏幕截图:http://cl.ly/677a6dc40034f096697f
这是我用来对这三行代码的PHP代码:
<!-- The View -->
<h2>Security analysis</h2>
<?php echo securitycheck($html, $css, $js); ?>
–
// The controller
function securitycheck($html, $css, $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',
'expression',
'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, 0, -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, 0, -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
}
内容总结
以上是互联网集市为您收集整理的PHP strpos()返回奇数结果全部内容,希望文章能够帮你解决PHP strpos()返回奇数结果所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。