PHP script just DOES NOT want to work! HELP PLEASE!

Discussion in 'PHP' started by jis2507, Mar 8, 2010.

  1. #1
    Hi all!

    I've been playing around with PHP for a bot (recently learnt it and think it's awesome).
    I wanted to make a simple little "betting" game using sessions (you'll understand how it works by looking at the index file).

    No matter what I try it does not seem to want to work the way I intend it to. Because I'm new to PHP could somebody please tell me what I'm doing wrong? I've posted the files below.

    index.php file.
    <?php
    session_start();
    ?>
    <html>
    <body>
    Josh's Betting Game! <br><br>
    <br>
    <?php
    if (isset($_SESSION["result"]))
    echo $_SESSION["result"];
    ?>
    <br>
    You currently have $
    <?php
    if (isset($_SESSION["money"])) { 
    echo $_SESSION["money"];
    } else {
    $_SESSION["money"]=1000;
    echo $_SESSION["money"];
    }
    ?>
    <br><br>
    Enter an ammount below to bet it. The maximum you can win is two timeswhat you bet.
    <br>
    When you reach $3000 you win!
    <br>
    <br>
    <form action="bet.php" method="post">
    Bet Ammount: <input type="text" name="bet"><br>
    <input type="submit" value="bet">
    </form>
    </body>
    </html>
    PHP:

    bet.php file.
    <?php
    session_start();
    unset($_SESSION["result"]);
    if ($bet>$_SESSION["money"])
    die("You don't have this much money!");
    $bet=$_POST['bet'];
    $double=$bet*2;
    $ammount=rand(-$bet,$double);
    $_SESSION["money"]=$_SESSION["money"]+$ammount;
    
    
    if ($ammount<0) 
    $_SESSION["result"]="You LOST \$$ammount";
    
    
    
    if ($ammount=0) 
    $_SESSION["result"]="You didn't loose or make anything.";
    
    
    
    if ($ammount>0) 
    $_SESSION["result"]="You WON \$$ammount!";
    
    
    if ($ammount>3000) 
    $_SESSION["result"]="YOU HAVE REACHED \$3000! You have won!!";
    
    header("Location: index.php");
    
    ?>
    PHP:
    Any help would be HEAPS appreciated!
     
    jis2507, Mar 8, 2010 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    on bet.php line 5, try to put that line after session_start();
     
    bartolay13, Mar 8, 2010 IP
  3. jis2507

    jis2507 Greenhorn

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #3
    What exactly do you mean? Everything in bet.php is after session_start();

    I tried it anyway but it's still not working.

    Any more ideas??
     
    jis2507, Mar 8, 2010 IP
  4. Andrew E.

    Andrew E. Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Here is a quick fix that works for me:

    bet1.php
    
    <?php
    	session_start();
    	if ($_SESSION['money'] != '') {
    		if ($_SESSION['money'] < 0) {
    			$_SESSION['money'] = 0;
    			$money = $_SESSION['money'];
    		} else {
    			$money = $_SESSION['money'];
    		}
    	} else {
    		$_SESSION['money'] = 1000;
    		$money = $_SESSION['money'];
    	}
    ?>
    <html>
    <body>
    Josh's Betting Game! <br><br>
    <br>
    <?php
    if ($_SESSION["result"] != '')
    	echo $_SESSION["result"];
    ?>
    <br>
    You currently have $<?php echo $money; ?>
    <br><br>
    Enter an ammount below to bet it. The maximum you can win is two times what you bet.
    <br>
    When you reach $3000 you win!
    <br>
    <br>
    <form action="bet2.php" method="post">
    Bet Ammount: <input type="text" name="bet"><br>
    <input type="submit" value="bet">
    </form>
    </body>
    </html>
    
    PHP:
    bet2.php
    
    <?php
    session_start();
    
    if ($bet>$_SESSION["money"])
    	die('You don\'t have this much money!');
    
    $bet=$_POST['bet'];
    $double=$bet*2;
    $ammount=rand(-$bet,$double);
    
    $_SESSION['money']=$_SESSION['money']+$ammount;
    
    
    if ($ammount<0):
    	$_SESSION['result'] = "You LOST \$$ammount";
    endif;
    
    
    if ($ammount=0):
    	$_SESSION['result'] = "You didn't loose or make anything.";
    endif;
    
    if ($ammount>0):
    	$_SESSION['result'] = "You WON \$$ammount!";
    endif;
    
    
    if ($ammount>3000):
    	$_SESSION['result'] = "YOU HAVE REACHED \$3000! You have won!!";
    endif;
    
    
    header('Location: bet1.php');
    
    PHP:
    The only real difference was wrapping the result on the second page around complete if blocks. Works for me.
     
    Andrew E., Mar 8, 2010 IP
  5. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi

    You might be best putting everything in functions (or a class) as it's easier to debug when something doesn't work.

    I've actually quickly re-wrote your code in a class with functions; for an example on how to do something like this. Copy the below code into a file and name it bet.php - If you want to rename it something else, make sure the links are changed in the code.

    It works good for me; and probably a good example for anyone who want's to make a small PHP game.

    Hope it helps.

    
    <?php
    	session_start();
    	
    	if(isset($_GET['reset'])) {
    		session_destroy();
    		die('The game is now reset, please go <a href="bet.php">back and try again</a>.');
    	}
    	
    	if (isset($_SESSION['result'])) {
    		$betting = new BetGame($_SESSION['result']);
    	} else {
    		$betting = new BetGame(2000);
    	}
    	
    	if (isset($_POST['bet'])) {
    		$betting->placeBet($_POST['bet']);
    	}
    	
    	$_SESSION['result'] = $betting->totalAmount();
    	
    ?>
    <br />
    Enter an ammount below to bet it. The maximum you can win is two timeswhat you bet.
    <br />
    When you reach $3000 you win!
    <br />
    <br />
    <form action="bet.php" method="post">
    <label for"bet">Bet Amount:</label> <input type="text" name="bet"><br>
    <input type="submit" value="bet">
    </form>
    <br /><a href="bet.php?reset=true">[ Reset Game ]</a><br />
    
    <?php
    	echo '<br /><b>You currently have: $'.$betting->totalAmount().'</b>';
    
    	class BetGame {
    		var $total = 2000;
    		function __construct($total) {
    			if (isset($total)) {
    				$this->total = $total;
    			}
    		}
    		
    		function totalAmount() {
    			return $this->total;
    		}
    		
    		function placeBet($amount) {
    			if ($this->totalAmount() <= 0) {
    				$this->msg('<b style="color:red;">You have lost all your money! click <a href="bet.php?reset=true">here</a> to reset the game.</b>');
    				die();
    			}
    			if ($this->totalAmount() >= $amount) {
    				$this->total = ($this->totalAmount() - $amount);
    				$bet = $this->doBet($amount);
    				
    				if ($bet < 0) {
    					$this->total -= $amount;
    					$this->msg('You lost $'. $amount);
    				} elseif ($bet == 0) {
    					$this->msg('You didnt lose or make anything!');
    				} elseif ($bet > 0) {
    					$this->total += (($amount*2) + $amount);
    					$this->msg('You WON $'. ($amount*2));
    				}
    				
    				if ($this->totalAmount() >= 3000) {
    					$this->msg('<br /><b style="color:red;">YOU HAVE REACHED $3000, YOU HAVE WON!</b>'); 
    				}
    				
    			} else {
    				$this->msg('You do not have enough money!');
    			}
    		}
    		
    		function msg($err) {
    			echo $err;
    		}
    		
    		function dobet($amount) {
    			return mt_rand(($amount - $amount - $amount), $amount);
    		}
    	}
    ?>
    
    PHP:
    Regards,
    Steve
     
    Steve136, Mar 8, 2010 IP