会话 - 未定义的索引

会话 - 未定义的索引

问题描述:

When I run my slot machine, win the jackpot, and refresh the page or click on the button, it says:

Notice: Undefined index 'gokken'

But when refreshed again, the error disappears untill I get a new jackpot.
Why is that happening?

<?php
session_start();
//session_destroy();
class Player
{

    public $name;
    public $age;

    function __construct()
    {
        $this->name = 'Harm';
        $this->age = 18;
    }

    public function setName($name)
    {
        return $this->name = $name;
    }

    public function setAge($age)
    {
        return $this->age = $age;
    }

    public function getAge($age)
    {
        if ($age < 18)
        {
            return "U mag niet spelen! <br/>";
        } else
        {
            return 'Welkom ' . $this->name . ', u bent 18 jaar of ouder! <br/><br/>';
        }
    }

}

$player = new Player();

echo $player->getAge($player->age);

class Gokautomaat
{

    public function Automaat()
    {
        $r1 = rand(0, 1);
        $r2 = rand(0, 1);
        $r3 = rand(0, 1);
        $inzet = 100;
        echo $r1 . $r2 . $r3 . '<br/><br/>';


        if ($r1 == $r2 && $r3 == $r2)
        {
            echo 'U heeft de jackpot gewonnen!!<br><br> + €5000 <br/><br/>';
            $_SESSION["gokken"] = $_SESSION["gokken"] + 5000;
            echo 'Cash: $' . $_SESSION["gokken"];
            session_destroy();
        } else
        {
            //$_SESSION["gokken"] = $_SESSION["gokken"];
            echo "U heeft niks gewonnen. <br/><br/> Bedrag - €$inzet" . '<br/><br/>';
            $_SESSION["gokken"] = $_SESSION["gokken"] - $inzet;
            echo 'Cash: €' . $_SESSION["gokken"];
        }
    }

    public function Gokken()
    {
        if ($_GET["play"])
        {
            if (!isset($_SESSION["gokken"]))
            {
                $_SESSION["gokken"] = 1000;
            }

            echo '<form action=' . $_SERVER['PHP_SELF'] . ' method=get>';
            echo '<input type=hidden name=play value=gokken>
                 <input type=submit value="Gok!"></form>';
        } else
            {
            echo '<form action=' . $_SERVER['PHP_SELF'] . ' method=get>';
            echo '<input type=hidden name=play value=gokken>
                 <input type=submit value="Gok!"></form>';
            }

        exit();
    }

}

$gokautomaat = new Gokautomaat;

echo $gokautomaat->Automaat();
echo $gokautomaat->Gokken();
?>

It's because when you start it the first time the $_SESSION["gokken"] is not set. And as you see there are lines, which try to read that unset value:

$_SESSION["gokken"] = $_SESSION["gokken"] + 5000;

And that value is initialized in Gokken(), but that executes after Automaat():

echo $gokautomaat->Automaat();
echo $gokautomaat->Gokken();

And since this is a session variable, it remains set the next time. You can solve this multiple ways. For example make sure that the initialization executes first.

Or you can turn off the notices:

error_reporting(E_ALL & ~E_NOTICE);

Or you can just hide any errors for the specific lines, with the '@' operator:

@$_SESSION["gokken"] = $_SESSION["gokken"] + 5000;