Page 1 of 1

can't pass variable with $_SESSION

Posted: Wed Jan 05, 2011 1:36 am
by ldor
Hi all,

I have a very simple form, where the user just has to press a button several times. Each time a php script is called, which generates a random number and makes some calculations. So, I need to keep a count of how many times the user has pressed the button. I tried to use superglobal $_SESSION['...'] for that, but somehow that does not work. After pressing the button, when the script is called again, the variable appears to be not set.

Before, I used this approach successfully on pages that require user login. But this page does not require a user to login. So, I thought that this could be the reason because no session was created. Then I created a session explicitly using session_start() function. But even then I got the same result - when the script is called the second time the variable $_SESSION['...'] is not set, so no variable is passed to the second instance of the script.

Can anybody help with this?

Re: can't pass variable with $_SESSION

Posted: Wed Jan 05, 2011 2:43 am
by social_experiment
Do you start the session on the page that the user clicks? Paste your script :)

Re: can't pass variable with $_SESSION

Posted: Wed Jan 05, 2011 5:39 am
by ldor
Yes, on the same page. Here are the codes:

HTML:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-9"></head>
<body>

<form method="post" action="/i-ching.php">
<input name="submit" value="0" type="submit" />
</form>


</body>
</html>

PHP:
(it's not complete, just pay attention to the lines that contain $_SESSION['toss']. It is printed on the button and also using echo just to follow what's going on)

<?php
if(!isset($_SESSION['toss'])) {
session_start();
$_SESSION['toss']=0;
}

echo '<form method="post" action="/en/myphpcodes/i-ching.php">';
echo '<input name="submit" value='.strval($_SESSION['toss']).' type="submit" >';
echo '</form>';
$_SESSION['toss']++;

$first=rand(2,3); $second=rand(2,3); $third=rand(2,3);
$coins[$i][0]=$first; $coins[$i][1]=$second; $coins[$i][2]=$third;
echo('<img src="/en/images/portal/i-ching/coin'.$coins[$i][0].'.jpg"> <img src="/en/images/portal/i-ching/coin'.$coins[$i][1].'.jpg"> <img src="/en/images/portal/i-ching/coin'.$coins[$i][2].'.jpg">');
echo '<br>'.$_SESSION['toss'];


?>

Re: can't pass variable with $_SESSION

Posted: Wed Jan 05, 2011 6:35 am
by social_experiment
ldor wrote:it's not complete, just pay attention to the lines that contain $_SESSION['toss'].
Always paste a complete script, just because something seems insignificant doesn't mean it is :)

Always call session_start() on the top of the page

Code: Select all

<?php
session_start();
// rest of your code
?>
Why do you base your need for session_start() on the precense of $_SESSION['toss']? Call it at the top of each page, regardless of whether the variable is set or not.

Try setting $_SESSION['toss'] inside another type of input field.

Code: Select all

<?php echo '<input name="submit" value='.strval($_SESSION['toss']).' type="submit" >'; ?>
Instead use

Code: Select all

<?php echo '<input name="toss" value="'. $_SESSION['toss'] .'" type="hidden" >'; ?>
Now you can access that value via $_POST['toss'].

Hth

Re: can't pass variable with $_SESSION

Posted: Wed Jan 05, 2011 7:24 am
by ldor
Thanks a lot, now it seems to be working.
Why do you base your need for session_start() on the precense of $_SESSION['toss']?
In fact, in the beginning I did so, but something went wrong so I tried it this way. Now I again placed this function at the top of the page, and everything looks fine.
Always paste a complete script, just because something seems insignificant doesn't mean it is :)
I just menat that I did not complete it yet, so some parts look meaningless. Anyway, thank you once again for helping me with this.