Forum Moderators: coopster

Message Too Old, No Replies

Data inside $ SESSION variable disappears

         

rickibarnes

4:41 am on Dec 29, 2007 (gmt 0)

10+ Year Member



Hi,

My script takes data out of the $_POST array from a form on the previous page. I make it into a session variable like so:

$_SESSION['idNo'] = $_POST['idNum'];

$idNo = $_SESSION['idNo'];

So far so good; if I look at my session data with

Print_r ($_SESSION);

it appears like so:

[idNo] => 2

I also have links in the page, which allow the user to delete entries from a database according to their id number. The links look like so:

<a href="<?php echo $_SERVER['PHP_SELF'] . '?noticeid=' . $noticeid;?>">Delete condolence notice</a

My problem is, once the link is clicked, and the page reloads, the data in $idNo has disappeared, so

Print_r ($_SESSION);

then appears as:

[idNo] =>

I had thought that putting the data into a session variable would make it persist, but it seems this is not so? I'm thinking that having

$_SESSION['idNo'] = $_POST['idNum'];

execute again is the problem - is the (now empty) $_POST data writing over the previous data in the session variable?

If this is so, can anyone think of a way of circumventing this?

Any help would be greatly appreciated.

PHP_Chimp

7:32 pm on Dec 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As you have
$_SESSION['something'] = $_POST['something'];
If that $_POST['something'] = '';
Then the session variable will be set to '', as the value of that post variable will be blank when the page is reloaded.

You could use something like -


if (![url=http://uk.php.net/manual/en/function.empty.php]empty[/url]($_POST['whatever'])) {
$_SESSION['whatever'] = $_POST['whatever'];
}

Or you could use GET for your reload. As then when the page is requested first it will pass the POST variables over. The next time it is requested via GET these post variables shouldnt be over written, but you will be able to access a whole new array of GET's.
<a href="<?php echo $_SERVER['PHP_SELF'] . '?noticeid=' . $noticeid;?>

And you appear to be using GET for the noticeid in this example.

WesleyC

10:47 pm on Dec 29, 2007 (gmt 0)

10+ Year Member



Make sure you're calling session_start() in your PHP code before you use the $_SESSION variable, and that the directory set for PHP's session files is readable and writable by PHP. Alternately, write your own database session handler.

rickibarnes

2:55 am on Dec 31, 2007 (gmt 0)

10+ Year Member



Hi, thanks for the replies.

I'm passing the variables for the reload using GET now, like so:

<a href="<?php echo $_SERVER['PHP_SELF'] . '?noticeid=' . $noticeid&idNo=' . $idNo;?>

I just remembered that I faced exactly the same problem on another site a couple of years ago and fixed it in exactly the same way . . . guess I'm just a bit slow on the uptake . . .

(Quietly resolves not to waste any more of people's time with stupid questions to which she already knows the answer)