【问题标题】:Why my session is getting lost on the page redirect?为什么我的会话在页面重定向上丢失了?
【发布时间】:2014-10-31 05:41:32
【问题描述】:

我知道这又是同一个问题,但是我尝试了所有方法并排除了故障,但不明白为什么在这种情况下我会出错

提前致谢

loginpage.php

<?php 
   include('crud.php');
   // code
    if($_POST['login']=='ok'){      
        $r=base::login($_POST);
        if($r==true)
        {
            var_dump($_SESSION['login']); // here I am getting the session it on the same page 
            header("location: index.php");
            die();
        }
        else
        {
            $msg="Username or Password is Incorrect";
        }
    }
    // code
?>

index.php

<?php 
include('crud.php');
// code

var_dump($_SESSION['login']);  // here its lost

// code
?>

crud.php

<?php
@ob_start();
@session_start();
ini_set('error_reporting',1);
// db call
// other functions
$_SESSION['login']=$sql->id; // here session is created
return is_numeric($sql->id)? true :false;

?>

【问题讨论】:

  • 您遇到了什么错误?

标签: php session session-variables session-cookies


【解决方案1】:

在一个文件中使用 $_SESSION['loginId'],在其他文件中使用 $_SESSION['login']。

您还可以使用 var_dump($_SESSION) 查看 $_SESSION 数组的所有元素。

【讨论】:

    【解决方案2】:

    代码对我来说运行良好。即使在一个文件中使用 $_SESSION['loginId'] 后,'login' 密钥仍然在 $_SESSION 全局数组中可用。因此,在 index.php 中它会回显“login”键值 b'因为它是在 crud.php 中设置的。

    【讨论】:

      猜你喜欢
      • 2020-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-02
      • 2017-09-11
      • 2014-08-23
      • 1970-01-01
      相关资源
      最近更新 更多