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 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示