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

时间:2013-05-31 11:50:18

标签: php

我尝试了很多次......但它没有重定向到任何地方:

<?php
    session_start();
    if (!isset($_SESSION['username'])) {
        header("Location: LogIn.php");
        exit;
    }
    else{
        //Content of the webpage
    }
?>

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:0)

我测试了它,它适用于我。确保在header功能之前没有任何输出。它也可能由error_reporting引起。尝试使用error_reporting(0);

答案 1 :(得分:0)

尝试使用像这样的绝对URl:

<?php  
    session_start();  
    if (!isset($_SESSION['username'])) {  
        header("Location: http://www.mysite.com/LogIn.php");  
        exit;  
    }  
    else{  
        //Content of the webpage  
    }  
?>

要尝试的另一件事是确保没有首先发送标头;如果页面上有任何可见的错误,将会出现。

答案 2 :(得分:0)

首先检查session变量是否具有该值并输入if条件。如果可行,请尝试使用标题中的完整网址。