drwo32555 2019-05-11 19:13
浏览 109
已采纳

会话变量在不同页面上显示为空

Edit: after doing some trial and error stuff I notice that if I comment the following code:

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

the $to variable displays as intended. So my question is why can't both be at the same time used?

Original question:

I'm trying to send an email using the mail() function. In order to do that I pass the variable $_SESSION['emailfrompass'] across 2 pages but for some reason the variable is always empty. Even though there is another variable that I send with a message and I have no problems in receiving it, this is the only one that makes problems

The session_start() is set across all pages. I tried using

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

for finding the problem but no use. The variable is always empty

This is my enterEFPass.php file. On top I have

<?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
}?>

And this is the enterEFPass_route.php in which I instantiate the $_SESSION variables

<?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");
      }

  }
  ?>

I expect the $to variable to print on the screen but it always prints "not found" If I print $_SESSION['success'] there is no problems in that

  • 写回答

2条回答 默认 最新

  • douxian8883 2019-05-12 15:56
    关注

    The method session_unset() does not take any arguments. It is used to unset ALL session variables, thus it is unsetting the $_SESSION['success'] variable. See here for more details on that. Instead, use unset($_SESSION['success']); to unset a single session variable. Hope this helps!

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!