Trouble with sessions 2

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Gavski
Forum Newbie
Posts: 19
Joined: Thu May 16, 2002 2:30 pm
Location: UK

Trouble with sessions 2

Post by Gavski »

I want to post a variable to a page and for that page to make it a session variable like so-

<?php

session_start();
$_SESSION['per']=$_POST['per'];

echo $_SESSION['per'];

?>

(The variable per is posted from another page.)

this echo's the variable 'per' but not when the page is returned to, so the post is successful but the variable will not persist??

Anyone help on this??

thanks

Gav
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post by RandomEngy »

When you return to the page, there is no longer a $_POST['per'] . So when you do $_SESSION['per']=$_POST['per']; you are updating $_SESSION['per'] to a null value, thus it will not echo when you return to the page.

To fix this, change

Code: Select all

$_SESSION&#1111;'per']=$_POST&#1111;'per'];
to

Code: Select all

if( isset($_POST&#1111;'per']) )
  $_SESSION&#1111;'per']=$_POST&#1111;'per'];
Post Reply