Jump to content

more help with sessions


shawnm

Recommended Posts

I'm still battling with php sessions. Below is what was kindly recommended by someone in this forum and it is what I have done, changing the form variables and .php page names to match my own:

 

page1.php

Code:

<form action="page2.php" method="post">

<p>Name:</p><input type="text" name="name" />

<input type="submit" value="Proceed to next page" />

</form>

 

page2.php

Code:

<?php

session_start();

$_SESSION['name'] = $_POST['name'];

?>

<form action="page3.php" method="post">

<p>Age:</p><input type="text" name="age" />

<input type="submit" value="Proceed to next page" />

</form>

 

page3.php

Code:

<?php

session_start();

$_SESSION['age'] = $_POST['age'];

?>

<form action="final.php" method="post">

<p>Profession:</p><input type="text" name="pro" />

<input type="submit" value="Send mail" />

</form>

 

final.php

Code:

<?php

session_start();

$name = $_SESSION['name'];

$age = $_SESSION['age'];

$pro = $_POST['pro'];

//send mail containing above info, using mail() or whatever you prefer

?>

<p>Mail send successfully!</p>

 

 

First question: is the php code (session_start(); etc.) supposed to go at the beginning of the source code? Or should it be embedded in the form?

 

Secondly, I cannot for the life of me get the sessions to work. No matter what I do, the only info that gets eventually sent to my email is that which is taken from the third form (third page, one form per page). The info from the other two forms cannot seem to carry through. Can anybody shed some light on this?

 

Thanks yet again.

Cheers,

Shawn

Link to comment
https://forums.phpfreaks.com/topic/122965-more-help-with-sessions/
Share on other sites

session_start() can be anywhere in the script BEFORE you use any session function/variables and BEFORE anything is outputted to the browser. Typically it is put at the top to avoid worrying about these conditions (and it's where most people look when they are reading your script.)

 

As for it not seeming to work.... it should. Make sure you have errors set to E_ALL and go ahead and post your mail() function here (or whatever your method is) so we can see if it is an issue there. Also, be sure you have cookies enabled...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.