PHP头文件("location: test.php")不工作.但是java脚本重定向工作


php header("location: test.php") not working. But java script redirect working

php header("location: test.php")

不工作。但是java脚本重定向工作

我尝试了header()不重定向?

header("location: test.php");

但我尝试用Java脚本它的工作

window.location ="http://www.test.com/test.php"

有什么问题?请给出解决方案

在调用header之前没有echo(输出命令)

没有错误/警告消息

我的确切代码:

if($contlogin >0 && $LoginID!="")
{      
    $_SESSION['LoginID'] = $LoginID;
    if($_SESSION['currentUrl']) {
        header("location: http://".$_SESSION['currentUrl']);
    }
    else {
        if($LoginID==1) {
            header("location:admin/index.php");
        }
        else {
            header("location:dashboard.php");
        }
    }
}
else {   
    header("location:index.php?err=1");
}

header("location: test.php");之后放置一个exit()

检查php标签<?php前是否有空格

检查你的error_reporting是on还是off,如果是off,那就打开

听起来您的PHP可能有错误,可能是语法错误。从命令行你可以运行PHP的lint检查器来查找错误:

php -l scriptname.php

或者,在php.ini或在脚本运行时启用error_reporting可以帮助您弄清楚发生了什么。

如果你还没有检查它,你的web服务器的错误日志也可能提供线索,发生了什么

对不起,我不能评论,直到我得到我的代表,所以我必须张贴这个但是你的php开始标签可能在它之前有一个空格,这会导致标题不能工作…所以<?php前面可能有空格或其他东西

难道 l location:应该 l

header("Location:test.php");

也可以在header()之后使用exit()来避免任何问题,并按照另一个海报的建议停止执行脚本。

不工作的一些原因header();1. 如果PHP标签前有空格,它将不起作用2. 在header()前的Echo语句在header()后添加exit()或die()

您的PHP代码必须在header生效之前向页面发送一些信息。在我的例子中,在body部分有一些JavaScript代码。这会导致一些数据(尽管不是立即可见)导致位置不工作。

请检查您的日志中的一些条目,如:

[Mon Apr 13 13:53:29 2015] [error] [client 192.168.109.109] PHP Warning:  Cannot modify header information - headers already sent by (output started at /var/www/html/xxx.php:208) in /var/www/html/xxx.php on line 565, referer: https://www.acme.com/xxx.php

您需要添加ob_start();

链接:http://php.net/manual/en/function.ob-start.php