douyingtai6662 2013-07-02 06:10
浏览 63
已采纳

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

I have the problem with header..

I am working on login functionality... in that

1)If the user logged in with his/her credentials...

2)After that user try to open new tab and type the LoginViewController.php..

3)Here i need to redirect the user to previous logged in page..based on seesion active ..

4)But the header is not redirecting to loggedin.php page.. and showing some blank page..

 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 
         {       

         }?>

Here is 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条回答 默认 最新

  • dragon202076 2013-07-02 06:17
    关注

    Didnt went through the code but at first glance to your question I can suggest you three things ..

    1. session_start() should be first line after <?php

    2. Make sure noting is getting outputted before header() . It won't work then.

      From 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. Try enabling output buffering using ob_start after session_start and flush it in end using ob_flush

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于OPENCV的人脸识别
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!