fsockopen与if / else无法正常工作

时间:2011-11-25 20:05:04

标签: php fsockopen if-statement

我有这个PHP代码

    $fp = @fsockopen($_SERVER['REMOTE_ADDR'], 80, $errno, $errstr, 1);

if ($fp) {
    $url = "/";

    fputs($fp, "GET $url HTTP/1.1\r\nHost: {$_SERVER['REMOTE_ADDR']}\r\nConnection: close\r\n\r\n");
    $resp = '';

    while(!feof($fp)) {
        $resp .= fgets($fp, 1024);
    }

    if (preg_match('/^http\/1\.\d+ 401/i', $resp)) {
        echo "Requires authentication";
    } else {
        echo "No auth required.";
    }
} else {
    echo "Failed to connect on port 80, reason: $errstr";
}

我已经编辑过这行

    } else {
    echo "Failed to connect on port 80, reason: $errstr";
}

我刚写了两行来检查$ errstr#Connection拒绝

就像这样

        } else {
    if ($errstr != 'Connection refused') {
        echo "Ok;
    }  
}

但代码无法正常运行

你能检查一下是什么问题吗?

由于

0 个答案:

没有答案