Click to See Complete Forum and Search --> : Session passing through URL


psyke
June 13th, 2011, 06:51 PM
Hi, I'm struggling with the exact same thing (http://www.codeguru.com/forum/showthread.php?t=419897) so I thought I'd post here.

I want to preserve the session id, via URL transmission, across three pages, but I can only get it to last for two pages, even though the code is basically the same.

Once I go from the second page to the third, I get a new session id no matter what. Any help would be appreciated!

php v5.2.14 , session.auto_start is off


Here's page 1 (pledge1.php):

<?php
ini_set('session.use_cookies',0);
session_start();
?>

<html>
<body>


<?php

//if form has been submitted, validate data, store it in session, go to pledge review page
if (isset($_POST['ReviewPledge'])) {

$_SESSION['amount'] = $_POST['amount'];
$url="Location: pledge2.php?".htmlentities(session_name().'='.session_id());
session_write_close();
header("$url");
exit();
}
?>

<?php
echo "session_id(): ".session_id();
echo "<br>SID: ".htmlspecialchars(SID);
echo "<p>"
?>


<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
Monthly pledge amount:
<label>
<input type="text" name="amount" id="amount" size="2" maxlength="2" value="<?php echo htmlspecialchars($_SESSION['amount']); ?>">
</label>

<p>
<label>
<input style="font-size:20"; type="submit" name="ReviewPledge" value="Submit pledge data">
</label>
</form>


</body>
</html>



page 2 (pledge2.php):

<?php
ini_set('session.use_cookies',0);
session_id($_GET[htmlentities(session_name())]);
session_start();
?>


<?php
//if the 'Submit my pledge' button was clicked, proceed
// for some reason the stupid link action method isn't working! :S
if(isset($_POST['SubmitPledge'])) {
$url="Location: pledge3.php?".htmlentities(session_name().'='.session_id());
session_write_close();
header("$url");
exit();
}
?>

<html>
<body>

<?php
echo "session_id(): ".session_id();
echo "<br>SID: ".htmlspecialchars(SID);
echo "<p>"
?>

You have pledged: $

<?php
echo $_SESSION['amount'];
?>


<FORM METHOD="LINK" ACTION="pledge1.php">
<INPUT TYPE="submit" VALUE="Go back and change pledge info.">
</FORM>

<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input style="font-size:20"; type="submit" name="SubmitPledge" value="Submit my pledge">
</form>

<!--
<FORM METHOD="LINK" ACTION="<?php echo 'pledge3.php?'.htmlentities(session_name().'='.session_id()).'/'; ?>">
<INPUT TYPE="submit" VALUE="Submit my pledge">
</FORM>
-->

</body>
</html>



and here's page 3 (pledge3.php):

<?php
ini_set('session.use_cookies',0);
session_id($_GET[htmlentities(session_name())]);
session_start();
?>


<html>
<body>

<?php
echo "session_id(): ".session_id();
echo "<br>SID: ".htmlspecialchars(SID);
echo "<p>"
?>

Thank you for your monthly pledge of: $

<?php
echo $_SESSION['amount'];
?>

<p>e-mail confirmation to user</p>


</body>
</html>

PeejAvery
June 14th, 2011, 09:03 AM
[ split thread ]

PeejAvery
June 14th, 2011, 09:04 AM
Welcome to the forums, psyke. Please don't resurrect old threads. If you have a question, create a new thread.

To answer your question...Stop wasting your time trying to pass a URL variable across the pages. Just give your session a name before starting it. A cookie will automatically be generated to store your session data.

<?php
session_name('mySessionName');
session_start();
?>