Jump to content

[SOLVED] Help with game - variables not working


ryeman98

Recommended Posts

Hey there, I'm making a game and in certain parts of the script the variables aren't working.  I made a Rock, Paper, Scissors game and then just changed the script to make this one.  The rps game works perfectly but this one is having problems.

 

// GLOBAL VARIABLES
$global_id = 4;
$games = mysql_query("SELECT * FROM `games` WHERE id='$global_id'");
$global_row = mysql_fetch_array($games);
$global_name = $global_row['name'];
$global_image = $global_row['img_url'];
$global_cost = $global_row['cost'];
$global_max_prize = $global_row['max_prize'];
$global_description = $global_row['description'];
// END GLOBAL VARIABLES

if ($_GET['act'] == "sessions") {
session_destroy();
$_SESSION['bust_pageview'] = 0;
$_SESSION['bust_prize'] = $global_cost;

 

As you can see, it gets the info about the game from the db and then puts it into variables. Those variables get set but when it goes to bust.php?act=choose, the variables work fine. Once it goes to bust.php?act=result, the variables then become NULL for some reason.

 

And to make matters worse, those Sessions there aren't being set at all. Any ideas?

(Yes, I have session_start(); at the beginning of the script.)

Have you verified that session_start() is at the top of the page that is not getting the data?

 

And, that you're not running the session_destroy() command between those page changes?

 

It is at the top and it's the exact order that my other game was in and the other game works just fine.

 

No, it's only run before the game starts so that it clears the user's previous game sessions.

We need to see more of your code.

 

Ken

 

Alright, I'll just post the whole thing. It's a little messy tho ;)

 

<?php
ob_start();
session_start();
$username = $_COOKIE['username'];
include_once("../includes/config.php");

// GLOBAL VARIABLES
$global_id = 4;
$games = mysql_query("SELECT * FROM `games` WHERE id='$global_id'");
$global_row = mysql_fetch_array($games);
$global_name = $global_row['name'];
$global_image = $global_row['img_url'];
$global_cost = $global_row['cost'];
$global_max_prize = $global_row['max_prize'];
$global_description = $global_row['description'];
// END GLOBAL VARIABLES

if ($_GET['act'] == "sessions") {
session_destroy();
$_SESSION['bust_pageview'] = 0;
$_SESSION['bust_prize'] = $global_cost;
$user_eP = mysql_query("SELECT ePoints FROM `users` WHERE username='$username'");
$row = mysql_fetch_array($user_eP);
$new_eP = $row['ePoints']-$global_cost;

if ($row['ePoints'] < $global_cost) {
	header('Location: bust.php?act=notenough');
} else {
	$update = mysql_query("UPDATE `users` SET ePoints='$new_eP' WHERE username='$username'");
	header('Location: bust.php?act=choose');
} // End if

} elseif ($_GET['act'] == "resetsess") {
$_SESSION['bust_pageview'] = 0;
header('Location: bust.php?act=choose');
} // End if

$pagetitle = "Errion - ". $global_name ."";
include("../includes/header.php");
include("../includes/randomevents.php");
include("../includes/checklogin.php"); ?>

<h2>Double or Bust!</h2>
<?php
if ($_GET['act'] == "choose") {
echo $_SESSION['bust_pageview'];
echo $_SESSION['bust_prize'];
echo "<b>Current Pot:</b> ". $_SESSION['bust_prize'] ." ePoints";
echo "<form action='bust.php?act=result' method='post'>";
echo "<table align='center' width='20%' border='0'><tr><td align='center'>";
echo "<input type='radio' name='choice' value='heads'><br />Heads";
echo "</td><td align='center'>";
echo "<input type='radio' name='choice' value='tails'<br />Tails";
echo "</td></tr></table>";
echo "<div align='center'><input type='submit' value='Double or Bust!'></div>";
echo "</form>";

} elseif ($_GET['act'] == "result") {
$_SESSION['bust_pageview'] = $_SESSION['bust_pageview']+1;

if ($_SESSION['bust_pageview'] > 1) {
	echo "<a href='/rules.php'>No cheating!</a>";
} else {
	$user_choice = $_POST['choice'];
	$comp = array("heads",
				"tails");
	$rand_comp = rand(0, 1);
	$comp_choice = $comp[$rand_comp];

	echo "You chose: ". $user_choice ."<br />";
	echo "The correct answer is: <b>". $comp_choice ."</b><br /><br />";

	function userWin() {
		$_SESSION['bust_prize'] = $_SESSION['bust_prize']*2;
		echo "You win! You can keep playing or take your money.<br />";
		echo "The pot is now at: ". $_SESSION['bust_prize'] ." ePoints<br /><a href='bust.php?act=resetsess'>Continue</a><br /><a href='bust.php?act=collect'>Collect your winnings!</a>";
	} // End function

	function userLoss() {
		$_SESSION['bust_prize'] = 0;
		echo "You lose all of the money in the pot!<br /><a href='/games/game.php?id=".$global_id."'>Play Again?</a>";
	} // End function

	if ($user_choice == "heads" && ($comp_choice == "heads")) {
		userWin();
	} elseif ($user_choice == "tails" && ($comp_choice == "tails")) {
		userWin();
	} elseif ($user_choice == "heads" && ($comp_choice == "tails")) {
		userLoss();
	} elseif ($user_choice == "tails" && ($comp_choice == "heads")) {
		userLoss();
	} else {
		echo "There is a problem with the game. Please report this error!";
	} // End result check

} // End if

} elseif ($_GET['act'] == "collect") {
$_SESSION['bust_pageview'] = $_SESSION['bust_pageview']+1;
$prize = $_SESSION['bust_prize'];
$user_info = mysql_query("SELECT ePoints FROM `users` WHERE username='$username'");
$row = mysql_fetch_array($user_info);
$new_eP = $row['ePoints']+$prize;
$month = date("F");

// Highscores
$check_user_score = mysql_query("SELECT * FROM `highscore_bust` WHERE username='$username'");
$row2 = mysql_fetch_array($check_user_score);

if ($prize == 0) {
	echo "You didn't make the highscores, sorry!<br /><br />";
} elseif (!$row2['username']) {
	$insert_score = mysql_query("INSERT INTO `highscore_bust` (username, score, month) VALUES ('$username', '$prize', '$month')");
} elseif ($row2['score'] <= $prize) {
	$update_score = mysql_query("UPDATE `highscore_bust` SET score='$prize', month='$month' WHERE username='$username'");
}
// End Highscores

if ($_SESSION['bust_pageview'] <= 2) {
$update = mysql_query("UPDATE `users` SET ePoints='$new_eP' WHERE username='$username'");
}
echo "You won ". $prize ."eP! <a href='bust.php'>Play Again?</a>";

} elseif ($_GET['act'] == "notenough") {
echo "Sorry, you don't have enough eP to play this game!";

} else {
echo "<table width='60%' border='0'>";
echo "<tr><td align='left' valign='top'><img align='left' src='". $global_image ."' /></td><td align='left'><b>Cost:</b> ". $global_cost ."eP<br />
<b>Max. Prize:</b> ". $global_max_prize ."<br />
<a href='highscores.php?gameid=". $global_id ."'>Highscores</a></td></tr>";
echo "<tr><td></td><td align='left'>". $global_description ."<br /><br />";
echo "<form action='bust.php?act=sessions' method='post'><input type='submit' value='". $global_name ."' /></form></td></tr></table>";
} // End if
?>

<?php include("../includes/footer.php"); ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.