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?
can't pass variable with $_SESSION
Moderator: General Moderators
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: can't pass variable with $_SESSION
Do you start the session on the page that the user clicks? Paste your script 
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: can't pass variable with $_SESSION
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'];
?>
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'];
?>
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: can't pass variable with $_SESSION
Always paste a complete script, just because something seems insignificant doesn't mean it isldor wrote:it's not complete, just pay attention to the lines that contain $_SESSION['toss'].
Always call session_start() on the top of the page
Code: Select all
<?php
session_start();
// rest of your code
?>Try setting $_SESSION['toss'] inside another type of input field.
Code: Select all
<?php echo '<input name="submit" value='.strval($_SESSION['toss']).' type="submit" >'; ?>Code: Select all
<?php echo '<input name="toss" value="'. $_SESSION['toss'] .'" type="hidden" >'; ?>Hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: can't pass variable with $_SESSION
Thanks a lot, now it seems to be working.
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.Why do you base your need for session_start() on the precense of $_SESSION['toss']?
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.Always paste a complete script, just because something seems insignificant doesn't mean it is