【发布时间】:2018-06-07 17:48:43
【问题描述】:
我正在使用 Slim 框架,如果用户丢失了会话,我想将用户重定向到登录页面,但我总是收到 SyntaxError : Unexpected token
我在 php 中的会话验证代码是这样的:
private function _validaSessao() {
$user = $this->userData['IdUser'];
if(null === $user || trim($user) == '') {
header("Location: http://192.168.0.9/", true, 301);
die();
}
}
我已经尝试过以下所有方法:
header('refresh:5;url=http://192.168.0.9/');
echo '<script>window.location.href = "http://192.168.0.9/";</script>';
return('<script>window.location.href = "http://192.168.0.9/";</script>');
echo json_encode('<meta HTTP-EQUIV="REFRESH" content="0; url=http://192.168.0.9/">');
我都试过了,我总是得到
200 ---- SyntaxError: Unexpected token < in JSON at position 0
唯一对我有用的代码是:
echo json_encode(array(
'SemSessao' => true
));
但是上面的代码让我检查 JavaScript 上的每一个调用,我想要一个 PHP 会重定向我的解决方案。这样我就不需要继续检查每个 JS 调用(很多),并且每次实例化一个 php 对象时,它都会检查会话并在不使用 JS 的情况下重定向用户。
更新 1 - 包含 JS 代码(到处都是可爱的反对票:D)
getDadosPlaneamento: function() {
var req = {Rota: '/planeamento/getDados/AUTO'};
var dfd = $.Deferred();
$.when(App.gajax(req)).done(function(d) {
On.Planeamentos = d.Planeamentos;
dfd.resolve();
});
return dfd.promise();
},
上面的代码是指我的php路由然后:
$onapp->get('/planeamento/getDados/:tipo/', function($tipo) {
if ($tipo == 'AUTO') {
$P = new MongoApi\Planeamento();
$ret = array(
$P->getAllMongo();
);
}
echo json_encode($ret);
});
当我执行$P = new MongoApi\Planeamento(); 时,我会使用 _validaSessao(); 检查用户在构造函数上是否有有效会话;
【问题讨论】:
-
@wayneOS 谢谢,反正那个也不行……
-
对不起,我误会了。你需要在你的脚本上设置你的
header()才能工作。