使用 php header('location') 刷新带有 javascript 的页面


Refreshing a page with javascript using php header('location')

我想使用一个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 处理标头?

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

PHP

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

爪哇语

window.location.reload();

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

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