头(QUOT;位置:/");重定向本地主机的作品,而不是远程服务器上 [英] header("Location:/"); redirect works on localhost, but not on remote server

查看:132
本文介绍了头(QUOT;位置:/");重定向本地主机的作品,而不是远程服务器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 如果(条件)
{
#大声笑。有些code在这里
}
其他
{
标题(位置:/); //我试图重定向到根
}

重定向完美的作品在本地主机上,而不是远程服务器上。
可这一切都最好使用 $ _ SERVER
如果我在同一个目录与文件重定向文件选择这个重定向甚至不工作。
希望大家帮我:)


解决方案

手册


  

HTTP / 1.1需要一个绝对URI作为参数传递给»位置:包括方案,主机名和绝对路径,但一些用户接受相对URI。通常,您可以使用 $ _ SERVER ['HTTP_HOST'] $ _ SERVER ['PHP_SELF'] 目录名()来从一个相对做出绝对URI自己:


 < PHP
/ *重定向到不同的页面被请求的当前目录* /
$主机= $ _ SERVER ['HTTP_HOST'];
$ URI = RTRIM(目录名($ _ SERVER ['PHP_SELF']),'/ \\\\');
额外的$ ='mypage.php';
标题(位置:http:// $ HOST $ URI / $额外);
出口;
?>

if (condition)
{
#lol. Some code here
}
else
{       
header("Location:/");//i'm trying to redirect to the root
}

Redirect works perfectly on localhost, but not on remote server. May be it's all better to use $_SERVER? This redirect wouldn't work even if i choose file in the same directory as file with redirect. Hope you help me :)

解决方案

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

这篇关于头(QUOT;位置:/&QUOT;);重定向本地主机的作品,而不是远程服务器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆