PHP Session Variable Passing

18,258

"session_start() is used in PHP to initiate a session on each PHP page. It must be the first thing sent to the browser, or it won't work properly, so it's usually best to place it right after the <?php tag. This must be on every page you intend to use sessions on."

http://php.net/manual/en/function.session-start.php

Share:
18,258
Spencer May
Author by

Spencer May

Updated on June 04, 2022

Comments

  • Spencer May
    Spencer May almost 2 years

    I can't seem to see why the $_SESSION['email'] isn't being passed

    Page 1 Segment

    <?php
    if (isset($finalusername, $finalpass, $finalemail)) {
    $myFile = "users/$finalusername.txt";
    $fh = fopen($myFile, 'w') or die("There was an error in creating your account.  <br />");
    $stringData = "$finalusername\n";
    fwrite($fh, $stringData);
    $stringData = "$finalpass\n";
    fwrite($fh, $stringData);
    $stringData = "$finalemail\n";
    fwrite($fh, $stringData);
    fclose($fh);
    
    // set session variable
    session_start();
    $_SESSION['email'] = "$finalemail";
    
    echo "<a href='emailverify.php'><button>Continue to Email Verification Page</button></a>";
    }
    ?>
    

    Page 2 Segment

    <?php
    
    // Check if your session variable is active
    session_start();
    if (isset($_SESSION['email'])) {
    
    $message = rand(111111111, 999999999);
    $to = "[email protected]";
    $subject = "Test mail";
    $from = "[email protected]";
    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);
    session_start();
    unset($_SESSION['email']);
    }
    ?>
    

    I really cant see what i've done wrong. I know everything else is eprfect because the rest of the code works.