简体   繁体   English

将未登录的用户重定向到登录页面

[英]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.

相关问题 从登录页面限制或重定向登录的用户 - Restrict or redirect logged in users from login page WordPress 将非登录用户的页面重定向限制为使用 PHP 登录页面 - WordPress restricted page redirect for non logged users to login page with PHP 如何将未登录的用户重定向到 Wordpress 上的登录页面? - How to redirect non logged in users to Login page on Wordpress? Bigcommerce-如果未登录,如何将用户从首页重定向到登录页面 - Bigcommerce - How to redirect users to login page from homepage if not logged in 如何将未登录的用户重定向到登录页面并重定向到保存数据的另一个页面? - how to redirect non logged users to login page and redirect to another page with saved data? 将用户重定向到登录页面 - redirect users to the login page 如果用户未登录,重定向到登录页面? - redirect to login page if user is not logged in? 如果用户未登录,则重定向到登录页面 - Redirect to login page if user is not logged in 如果未登录,则将登录页面重定向到登录页面 (tanyacms) - Redirect landing page to login page if not logged in (tanyacms) CakePHP 3-如何阻止未登录用户的页面并将其重定向到登录页面? - CakePHP 3 - How do I block pages from non logged in users and redirect them to the login page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM