加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – header(“Location:/”); redirect适用于localhost,但不

发布时间:2020-12-13 13:48:54 所属栏目:PHP教程 来源:网络整理
导读:if (condition){#lol. Some code here}else{ header("Location:/");//i'm trying to redirect to the root} 重定向在localhost上完美运行,但在远程服务器上运行不正常. 使用$_SERVER可能更好吗? 即使我选择与具有重定向的文件位于同一目录中的文件,此重定向
if (condition)
{
#lol. Some code here
}
else
{       
header("Location:/");//i'm trying to redirect to the root
}

重定向在localhost上完美运行,但在远程服务器上运行不正常.
使用$_SERVER可能更好吗?
即使我选择与具有重定向的文件位于同一目录中的文件,此重定向也不起作用.
希望你帮帮我:)

从 manual:

HTTP/1.1 requires an absolute URI as argument to ? Location: including the scheme,hostname and absolute path,but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'],$_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:

<?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;
?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读