使用php标题刷新页面('location')

时间:2012-07-01 17:25:42

标签: php javascript http-headers refresh

我想使用一个php文件来处理我的所有投票请求。

目前,如果未设置siteType,脚本将转发用户并显示消息。如果用户有JS,那么它将返回一个json对象和ajax页面。

        if(!isset($_COOKIE['siteType'])){
            displayMessages('bad', 'Before you can continue you must select which category you would like to be in.');
            header('Location:/');
            exit;
        }

我需要它,因为如果执行上面的PHP代码页面将重新加载,我假设使用javascript并读取http标题?

[编辑] 我没有让自己清楚,但这是一个ajax请求。我不输出任何HTML。所以这真的只是让js处理标题?

3 个答案:

答案 0 :(得分:3)

你不能使用php header('location')使用javascript刷新页面 因为,标题('Location:xxx');必须是PHP脚本的唯一输出,它是一个标题,你不能把它放在javascript语法之后

答案 1 :(得分:1)

PHP

echo '<meta http-equiv="refresh" content="0">';

的Javascript

window.location.reload();

答案 2 :(得分:0)

也许这对你来说是一个解决方案,

if(!isset($_COOKIE['siteType'])){
     displayMessages('bad', 'Before you can continue you must select which category you would like to be in.');
     echo '<script>window.location.reload()</script>';
     exit;
 }