3

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.

6
  • 8
    session_start() should be always placed on the top of PHP script Commented Jan 25, 2012 at 23:24
  • 1
    call session_start() always at the very beginning (just to be sure). Also the second call to session_start() in the page 2 script isn't neccessary. Commented Jan 25, 2012 at 23:26
  • Yupp, @MarekSebera, that's it. Also, no multiple calls will help. Commented Jan 25, 2012 at 23:26
  • Wow...is that really all? lol That was really simple goodness im a noob :( Thanks everyone though! Commented Jan 25, 2012 at 23:30
  • How come session_start() should be placed on top? 100% wrong , he isnt sending anything to the browser unless fopen didnt work or errors appear. Also you can use ob_start() and you can place session_start() anywhere you want. I don't see anything that will be sent to the browser b4 session_start() in that script. Commented Jan 26, 2012 at 9:06

1 Answer 1

4

"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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.