4 Replies - 885 Views - Last Post: 10 September 2011 - 05:36 AM Rate Topic: -----

#1 brett84c  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-September 11

Session won't work (beginner level problem)

Posted 09 September 2011 - 09:21 AM

For my first project in my PHP class, i have to make a betting game. Basically, the first screen shows 2 random numbers, low and high (generated by a rand number function), the first number being anything between 1 and 14($_Session['rand1']), the second being between the first random number and 14 ($_Session['rand2']). If "BET" is clicked, it's supposed to take you to the 2nd screen, where a 3rd random number ($guess) is checked to see if it's within or outside of the low and high session numbers from the first screen. The numbers from the first screen should display on the 2nd screen along with the 3rd guess and show whether it was within the low and high card (win) or outside (lose). The "NEXT DRAW" button should be the only selectable button on the 2nd screen, which takes you back to screen 1 with 2 new random low and high cards.

My problem is the session won't carry over (i.e. display on the second screen). I've tried putting the session variables into $low and $high variables but it still won't work. Anyone know what i'm doing wrong or lead me in the right direction? Here's my code (within the body tags):


<form method="post">
<?php
session_start();
$nextstep=$_POST['nextstep'];
if(!$nextstep||$nextstep=='NEXT DRAW')
{
	$_SESSION['rand1']=rand(1,14);
	$_SESSION['rand2']=rand($_SESSION['rand1'],14);
	$low=$_SESSION['rand1'];
	$high=$_SESSION['rand2'];
	?>
        <h1>Screen 1</h1><br />
        Low Card: <?php print $_SESSION['rand1']; ?><br /><br />
        High Card: <?php print $_SESSION['rand2']; ?><br /><br />
        <input type="submit" value="BET" name="nextstep" />
	<input type="submit" value="PASS" name="nextstep" /><?php
}
if($nextstep=='BET')
{
	$guess=rand(1,14);
	if($guess>=$_SESSION['rand1']&&$guess<=$_SESSION['rand2'])
	{
		?><h1>Screen 2</h1><br />
    		Low Card: <?php print $low;?><br /><br />
    		High Card: <?php print $high;?><br /><br />
            Target: <?php print $guess; ?><br />
			<?php print "Yes!";
	}
	else
	{
		?><h1>Screen 2</h1><br />
    		Low Card: <?php print $low;?><br /><br />
    		High Card: <?php print $high;?><br /><br />
            Target: <?php print $guess; ?><br />
			<br /><?php print "You Lose!";
	}
	?><br /><input type="submit" value="NEXT DRAW" name="nextstep" /><?php
}
else if($nextstep=='PASS')
{
	$_SESSION['rand1']=rand(1,14);
	$_SESSION['rand2']=rand($_SESSION['rand1'],14);
      ?>
     <h1>Screen 1</h1><br />
     Low Card: <?php print $_SESSION['rand1']; ?><br /><br />
     High Card: <?php print $_SESSION['rand2']; ?><br /><br />
    <input type="submit" value="BET" name="nextstep" />
    <input type="submit" value="PASS" name="nextstep" /><?php
}


?>



Is This A Good Question/Topic? 0
  • +

Replies To: Session won't work (beginner level problem)

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Session won't work (beginner level problem)

Posted 09 September 2011 - 09:39 AM

You can't display or output anything to screen before you call session_start(). Session_start() sends page headers so if you print anything (like you are doing when you print your form tag) it will cause the page to flush its headers and session_start() will not start a session for you. Don't even print a space. Make your <?php the first line of the file.

So move your form tag down to somewhere after session_start(). Your first two lines of the file should be <?php followed by session_start(). Then try again and you may see your sessions work.

If you still see nothing, make sure that the code that is setting your session variables is reached. If it is, then check to make sure that your PHP install has session.use_trans_sid set (you see this in PHP.ini).

If that is set to on and you still don't see the info, then check to make sure you accept cookies on your machine. Sessions still use cookies to transport the session id to keep your session going between pages.

Let us know how it turns out for you. :)
Was This Post Helpful? 0
  • +
  • -

#3 brett84c  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-September 11

Re: Session won't work (beginner level problem)

Posted 09 September 2011 - 09:59 AM

So i tried moving all the code above the header tags, but now the code only runs once, no matter what i do or press, and even when i get to the second screen, it's still blank. Do i need to set $_Sessions to variables or do i need to display those variables below the header tags?

When i say it's blank on the second screen, i mean the 2nd screen comes up, but there's no numbers where they're supposed to be
Was This Post Helpful? 0
  • +
  • -

#4 brett84c  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-September 11

Re: Session won't work (beginner level problem)

Posted 09 September 2011 - 10:55 PM

Alright, i met with my professor and he told me i had to keep the html tags within the session_start()... if it's outside, it causes the session to be purged because of the tags. Maybe that's what you were getting at earlier but it didn't make sense until my teacher showed me what's up. Thank you for the help!
Was This Post Helpful? 0
  • +
  • -

#5 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 946
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Session won't work (beginner level problem)

Posted 10 September 2011 - 05:36 AM

That is true but ultimately you should keep HTML separate from PHP.

PHP - Processes data
HTML - Shows data
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1