[英]Redirect the not logged in users to login page
I tried many many times... But it does not redirect to anywhere: 我尝试了很多次...但是它不会重定向到任何地方:
<?php
session_start();
if (!isset($_SESSION['username'])) {
header("Location: LogIn.php");
exit;
}
else{
//Content of the webpage
}
?>
How can I fix this? 我怎样才能解决这个问题?
I tested it and it works for me. 我测试了它,对我有用。 Be sure not to have any output before the
header
function. 确保
header
功能之前没有任何输出。 It can also be caused by error_reporting
. 也可能由
error_reporting
引起。 Try using error_reporting(0);
尝试使用
error_reporting(0);
. 。
Try using an absolute URl like this: 尝试像这样使用绝对URl:
<?php
session_start();
if (!isset($_SESSION['username'])) {
header("Location: http://www.mysite.com/LogIn.php");
exit;
}
else{
//Content of the webpage
}
?>
Another thing to try is make sure no headers are being sent first; 要尝试的另一件事是确保没有先发送头。 there will be a visible error if any are on the page.
如果页面上有任何可见错误。
首先检查session
变量是否具有该值并输入if
条件, if
可以,请尝试在标头中使用完整的url。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.