重定向无法正常工作


Redirect not working properly

这是一个非常愚蠢的问题,但不知何故它不起作用我有一个创建文件的函数,如果它完成了,我希望它将用户重定向到X页面。。在本例中是1.php……但不知何故不起作用:S为什么?

    //Creates File, populates it and redirects the user.
if (createfile($dbFile)) { 
    header('Location: 1.php');
        }

发送重定向标头后需要exit()

 if (createfile($dbFile)) {
     header('Location: http://yoursite.com/path/to/1.php', true, 302);
     exit();
 }

否则,PHP将继续执行。如果是exit(),则客户端会在您调用header()之后立即接收标头。

您还应该注意header():的PHP文档页面上的警告

HTTP/1.1需要一个绝对URI作为Location的参数:包括方案、主机名和绝对路径,但有些客户端接受相对URI。您通常可以使用$_SERVER['HTTP_HOST']$_SERVER['PHP_SELF']dirname()自己从相对URI中生成一个绝对URI:

<?php
/* Redirect to a different page in the current directory that was requested */
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/''');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
?>

您尝试过使用绝对位置吗?

$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/''');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;

Try;

if (createfile($dbFile)) { 
   header("Refresh: 0; URL=1.php");
   exit();
}

我遇到了一个类似的发音问题,代码在标头位置之后仍在执行。这就是为什么我总是做exit();之后