[英]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()
进行调用之后,客户端会立即收到该头。
HTTP / 1.1需要绝对URI作为Location的参数:包括方案,主机名和绝对路径,但是某些客户端接受相对URI。 通常,您通常可以使用
$_SERVER['HTTP_HOST']
,$_SERVER['PHP_SELF']
和dirname()
从您自己的亲戚那里创建绝对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;
尝试;
if (createfile($dbFile)) {
header("Refresh: 0; URL=1.php");
exit();
}
我有一个类似的听起来的问题,即在标头位置之后仍在执行代码。 这就是为什么我总是这样做exit(); 然后
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.