2013-07-02 50 views
0

我有头问题不能正常工作..重定向使用header()函数,同时检查会话

我工作的登录功能......在

1)如果用户登录自己的/她的证书......

2)用户后尝试打开新的标签页,然后键入LoginViewController.php ..

3)在这里,我需要将用户重定向到以前的登录page..based上seesion 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... 
+0

格式的代码正确 –

回答

1

Didnt通过代码去,但乍一看你的问题,我可以建议你三样东西..

  1. session_start()应该是第一行后<?php

  2. 确保提的是越来越输出前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. 
    
  3. 尝试启用使用ob_start输出缓冲session_start后,用ob_flush

+0

好吧,我得到了结果....谢谢你快速回复.. –

0

把在session_start()顶端。它应该是第一行,并从代码中删除不必要的空间。

+0

在结束它刷新,但是它需要一些声誉点...我没有那些.. –

+0

@KVK_cool ohk没问题.... :-) –

2

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

+0

这意味着在包括顶部... –

+0

@KVK_cool是的,你是对的 – alwaysLearn

+0

我得到了结果谢谢.. –