空子串,这里有什么问题?

时间:2011-04-07 06:10:19

标签: php

警告:substr_count()[function.substr-count]:空子串。

调用函数browsertest时收到此消息。这是函数的一部分,它告诉我这里有错误

if(substr_count($referer, $tld[$i]) > 0)

该功能显示在下方。感谢您对我的问题所花时间和兴趣。

function testbrowser()
{
    //check for browser type
    $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);

    //set safe browser types
    $browser[0] = "msie 6";
    $browser[1] = "msie 7";
    $browser[2] = "firefox";
    $browser[3] = "safari";

    $i = 0;
    $user_agent_ok = 0;
    while($i < 4)
    {
        if(strpos($useragent, $browser[$i]) > 0)
        {
            $user_agent_ok = 1;
            $i=4;
        }
        $i++;
    }

    if ($user_agent_ok == 1)
    {
        //check referer
        $safe = 0;
        $query  = "SELECT * FROM safe_referers";
        $result = mysql_query($query);

        //check for blank referer
        if ($_SERVER['HTTP_REFERER'] != ""  AND (substr_count($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME']) == 0))
        {
            //strip down referer information to http://subdomain.domain.tld
            $tld[0] = ".com";
            $tld[1] = ".net";
            $tld[2] = ".org";
            $tld[3] = ".info";
            $tld[4] = ".co.uk";
            $tld[5] = ".co.in";
            $tld[6] = ".it";
            $tld[8] = ".gr";
            $tld[9] = ".no";
            $tld[10] = ".de";
            $tld[11] = ".ru";
            $tld[12] = ".pl";
            $tld[13] = ".dk";
            $tld[14] = ".pt";
            $tld[15] = ".ca";

            $i = 0;
            while($i < 16)
            {
                $referer = strstr ($_SERVER['HTTP_REFERER'], "http://");
                if(substr_count($referer, $tld[$i]) > 0)
                {
                    $referer = substr($referer, 0, strpos($referer, $tld[$i]));
                    $referer = $referer . $tld[$i];
                    $i=16;
                }
                $i++;
            }

3 个答案:

答案 0 :(得分:4)

缺少

$ tld [7]。循环遍历它们时,您将获得一个空的子字符串

现在有时它会在你到达$ i == 7之前匹配。然后就不会发生错误。

您可以使用

$tld = array_values($tld);
while ($i < count($tld)) {

或代替使用

foreach ($tld as $v) {

循环可能如下所示

$tld = array(".com",".net",".org",".info",".co.uk",".co.in",".it",".gr",".no",".de",".ru",".pl",".dk",".pt",".ca");

foreach ($tld as $v)
{
    $referer = strstr ($_SERVER['HTTP_REFERER'], "http://");
    if(strstr($referer, $v))
    {
        $referer = substr($referer, 0, strpos($referer, $v));
        $referer = $referer . $v;
        break;
    }
}

但这就是我写它的方式

$tld = array(".com",".net",".org",".info",".co.uk",".co.in",".it",".gr",".no",".de",".ru",".pl",".dk",".pt",".ca");
foreach ($tld as $v)
{
    if (preg_match('#\\'.$v.'/#',$_SERVER['HTTP_REFERER'])) {
        $referer = preg_replace('#(^http(s*)://[^/]*).*#','\1',$_SERVER['HTTP_REFERER']);
        break;
    }
}

答案 1 :(得分:2)

这是因为您忘记设置$tld[7]并且已禁用详细警告。

重写内部循环,如下所示:

$tlds = array('.com', '.net', '.org', '.info', '.co.uk', '.co.in', '.it', '.gr',
              '.no', '.de', '.ru', '.pl', '.dk', '.pt', '.ca');

foreach ($tlds as $tld) {
   $referer = strstr ($_SERVER['HTTP_REFERER'], "http://");
   $p = strpos($referer, $tld);
   if ($p !== false) {
       $referer = substr($referer, 0, $p) . $tld;
   }
}

答案 2 :(得分:0)

如果您没有通过点击链接访问该页面,则

$_SERVER['HTTP_REFERER']为空,如果客户端(浏览器)没有首先发送该链接,则它也可以为空see the documentation for $_SERVER < / p>