检查会话时使用header()的重定向无法正常工作

时间:2013-07-02 06:10:46

标签: php header

我有标题问题..

我正在处理登录功能...

1)如果用户使用他/她的凭证登录......

2)在该用户尝试打开新选项卡并键入LoginViewController.php ..

之后

3)在这里,我需要将用户重定向到之前登录的页面...基于seeion active ..

4)但是标题没有重定向到loggedin.php页面..并显示一些空白页面..

 Here is the LoginViewController.php

 <?php
 session_start();
 include('GenericClasses/GenericCollectionClass.php');
 include('Models/UsersModel.php');
 include('DataObjects/Users.php');
 include('DatabaseAccess/DBHandler.php');

     if(!empty($_SESSION['user']))// Here checking session is empty or not
        {
            header("Location : loggedin.php");// Here it is not redirecting properly
            die();
        }
        else 
         {       

         }?>

这是loggin.php

<?php
  session_start();
  header("Cache-Control: private, must-revalidate, max-age=0");
  header("Pragma: no-cache");
  header("Expires: Fri, 4 Jun 2010 12:00:00 GMT");
  include('GenericClasses/GenericCollectionClass.php');
  include('Models/UsersModel.php');
  include('DataObjects/Users.php');
  include('DatabaseAccess/DBHandler.php');

      if(!isset($_SESSION['user']))
         {
           header('Location: LoginViewController.php');
           exit();
          }
          echo '<div style="background:white; text-align:right"> Login  as:'.$_SESSION['user'].'<a href="LogoutViewController.php" style="text-align:right">Logout</a></div>';
         ?>


Any suggestions are acceptable... 

3 个答案:

答案 0 :(得分:2)

session_start();应该是第一行。在输出任何内容后,您也无法进行重定向,这包括所包含文件中的任何尾随空格。

答案 1 :(得分:1)

Didnt完成了代码,但乍一看你的问题我可以建议你三件事。

  1. session_start()应该在<?php

  2. 之后的第一行
  3. 确保在header()之前输出注释。那不行。

    来自documentation

    Remember that header() must be called before any actual output is sent, 
    either by normal HTML tags, blank lines in a file, or from PHP. 
    
  4. 尝试在session_start后使用ob_start启用输出缓冲,然后使用ob_flush

  5. 将其刷新

答案 2 :(得分:0)

将session_start()放在顶部。它应首先在第一行,并从代码中删除不必要的空间。

相关问题