gpt4 book ai didi

PHP strpos() 返回奇怪的结果

转载 作者:可可西里 更新时间:2023-10-31 23:11:40 27 4
gpt4 key购买 nike

我已经为我的网络应用编写了一个基本的“安全检查程序”。我需要看一眼用户提交的代码是否包含恶意内容。

这是我现在正在运行的代码的屏幕截图: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() 来做,而且它报告了两个 alert,正如你从第一个中看到的那样屏幕截图,只有一个通过此运行。

我做错了什么?

谢谢!

jack

编辑正如 Fosco 友善地指出的那样,alert 在我的数组中列出了两次(doh!)。我已经解决了这个问题,但是留下尾随逗号的问题仍然存在。我知道这是一个较小的问题,但我发誓它仍然不应该存在......

最佳答案

一眼看去,您的代码应该会提供您想要的输出。我不确定出了什么问题。

与其将 $found 构建为字符串,我建议将其构建为数组,然后使用 implode() 获取字符串:

  • $found = ""; 替换为 $found = array();
  • 替换$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() 返回奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3477228/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com