2013-07-02 17 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

我得到了結果謝謝.. –