代码之家  ›  专栏  ›  技术社区  ›  Jack marksy

PHP strpos()返回奇数结果

  •  4
  • Jack marksy  · 技术社区  · 15 年前

    这是一个截图 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 从第一张截图中你可以看到,只有一张是这样的。

    谢谢!

    杰克

    编辑: 正如Fosco善意地指出的, 在我的数组中列出了两次(doh!)。我已经解决了这个问题,但是后面逗号的问题仍然存在。我知道这是一个小问题,但我发誓它仍然不应该在那里。。。

    4 回复  |  直到 15 年前
        1
  •  1
  •   Hammerite    15 年前

    乍一看,您的代码似乎应该给出您想要的输出。我不确定出了什么问题。

    而不是建造 $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
    }
    
        2
  •  1
  •   Peter O'Callaghan    15 年前

    $found = array();
    
    foreach($insecure as $item)
    {
        if (($pos = strpos($code, $item)) !== FALSE)
        {
            $found[] $item;
        }
    }
    $found = implode(', ', $found);
    

    字符串中只有一个警报,但它在$unsecure列表中出现了两次,因此它在输出中出现了两次。为了避免这种情况,您必须分别扫描每个部分。

        3
  •  1
  •   Vladislav Rastrusny    15 年前

    不要重新发明轮子。

    http://htmlpurifier.org/

        4
  •  1
  •   Joseph    15 年前

    你试过只回显$found然后做一个查看源代码吗?我猜问题是您的一个项目没有基于HTML编码显示 <? ')并且有一个逗号和空格实际上正在被删除。

    但我会用他的解决方案来回应哈默莱特。