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 Answers

2
Matthew Vanlandingham On Best Solutions

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!

-2
user3637810 On

use session_start() only in file that you are going to set value of session, no need in all files, thats how i used it in my last projects for custom logins systems