2012-04-10 82 views
0

这是我第一次在这个网站上提问,所以请耐心等待。我从另一个网站获得了用于测试互联网连接速度的脚本,并添加了其他语句。如果速度超过500,它将重定向到特定页面。出于某些原因,我无法使其工作。我在<html>标记之前添加了ob_start();,并在</html>标记之后添加了ob_end_flush();。我在下面的代码中加入了我的body标签。我的PHP如果语句不起作用

$kb=512; 
flush(); 
$time = explode(" ",microtime()); 
$start = $time[0] + $time[1]; 

for($x=0;$x<$kb;$x++){ 
    echo str_pad('', 1024, ''); 
    flush(); 
} 
$time = explode(" ",microtime()); 
$finish = $time[0] + $time[1]; 
$deltat = $finish - $start; 
$intspeed = round($kb/$deltat, 0); 
echo $intspeed; //just to check if $intspeed has a value 

if ($intspeed > 500) { 
    header("Location: test.php"); 
    exit(); 
} else { 
    header('Location: falcons/index.php'); 
    exit(); 
} 
+3

在发送自定义标题之前,您无法在屏幕上打印任何内容(使用'echo')。您在屏幕上回显某些内容,然后发送重定向标头。那就是问题所在。删除回声。 – 2012-04-10 17:33:44

+1

幽默地说,'echo $ intspeed'会阻止重定向 – 2012-04-10 17:34:19

+0

我非常确定输出缓冲会绕过这个......当然,只有当代码位于ob_start()和ob_end_flush()之间时,OP说,他使用,而不是之前或之后。 – MichaelRushton 2012-04-10 17:34:55

回答

1

删除flush();来电。此外,请确保此代码位于ob_start()ob_end_flush()之间,而不是之前或之后(并且在此代码之前没有其他输出)。

$kb=512; 

$time = explode(" ",microtime()); 
$start = $time[0] + $time[1]; 

for($x=0;$x<$kb;$x++){ 
    echo str_pad('', 1024, ''); 
} 
$time = explode(" ",microtime()); 
$finish = $time[0] + $time[1]; 
$deltat = $finish - $start; 
$intspeed = round($kb/$deltat, 0); 
echo $intspeed; //just to check if $intspeed has a value 

if ($intspeed > 500) { 
    header("Location: test.php"); 
    exit(); 
} else { 
    header('Location: falcons/index.php'); 
    exit(); 
} 
0

您只能通过重定向头()如果输出尚未开始。如果已经有非标题输出(如在for循环中),设置“Location”标题不起作用。

我建议在设置“位置”标头之前使用headers_sent(),并在出现一些调试信息或其他已经开始输出的情况下进行回退。

+0

谢谢。它现在有效!我必须删除回显和flush()。 – Filap 2012-04-10 18:09:31