【问题标题】:Session variable appears empty on different page会话变量在不同页面上显示为空
【发布时间】:2019-05-11 19:13:37
【问题描述】:

编辑:在做了一些反复试验后,我注意到如果我评论以下代码:

 $success = $_SESSION["success"];
session_unset($_SESSION["success"]); 

$to 变量按预期显示。所以我的问题是为什么不能同时使用两者?

原问题:

我正在尝试使用 mail() 函数发送电子邮件。为了做到这一点,我将变量 $_SESSION['emailfrompass'] 传递到 2 个页面,但由于某种原因,该变量始终为空。即使我通过消息发送了另一个变量并且我在接收它时没有问题,但这是唯一产生问题的变量

session_start() 在所有页面中设置。我尝试使用

ini_set('display_errors',1); error_reporting(E_ALL);

用于发现问题但没有用。变量始终为空

这是我的 enterEFPass.php 文件。最重要的是我有

<?php
session_start();
ini_set('display_errors',1); 
error_reporting(E_ALL); 
?>
 <?php 


    if(isset($_SESSION["success"]))
    {
       $success = $_SESSION["success"];
       session_unset($_SESSION["success"]); //echoing this will display the right thing
       $to = "";
       $to  = isset($_SESSION['emailforpass']) ? $_SESSION['emailforpass'] : 'not found';
       echo $to; //does not work


       //mail($to, "Reset your password", $message, $headers);


   ?>

     <div id='alert'>
      <div class='alert alert-block alert-info'>
      <?php   
       echo $success;

      // echo "<script>setTimeout(\"location.href = 'login-page.php';\",2500);</script>";
    ?>
      </div>
     </div>
<?php
}?>

这是我实例化 $_SESSION 变量的 enterEFPass_route.php

<?php
  session_start();
  include 'db.php';


  $email = "";
  $conn = Connect();
 if (isset($_POST['send_email_button'])) 
 {
     $email = mysqli_real_escape_string($conn, $_POST['email']); //$_POST['email'] the field where I introduce the email
 }
 if (empty($email)) { array_push($errors, "Please enter a valid email"); }
 if (count($errors)==0) // just an array for error messages
    {

        $_SESSION['emailforpass'] = $email; //appears empty always
        $_SESSION['success'] = "An email has been sent to the corresponding address. Please follow the instructions provided there"; //goes without problems

        header("location: enterEFPass.php");


    } else
    {
        $_SESSION['errormsgpassress']= $errors; //no problems in sending
        header("location: enterEFPass.php");
      }

  }
  ?>

我希望 $to 变量可以打印在屏幕上,但它总是打印“未找到” 如果我打印 $_SESSION['success'] 那就没有问题了

【问题讨论】:

  • db.php 文件的用途是什么?是否包含连接数据库的mysqli_connect()函数?
  • @MatthewVanlandingham 是的,该文件用于连接

标签: php mysql session-variables


【解决方案1】:

session_unset() 方法不接受任何参数。它用于取消设置所有会话变量,因此它正在取消设置$_SESSION['success'] 变量。有关详细信息,请参阅here。相反,使用unset($_SESSION['success']); 取消设置单个会话变量。希望这会有所帮助!

【讨论】:

  • 谢谢,确实是这个问题。非常感谢
猜你喜欢
  • 2015-10-10
  • 2013-03-24
  • 2013-10-09
  • 2020-05-30
  • 1970-01-01
  • 2011-08-17
  • 1970-01-01
  • 2013-07-30
  • 2015-12-18
相关资源
最近更新 更多