dongqiao6445 2017-01-10 14:00
浏览 47
已采纳

会话 - 未定义的索引

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();
?>
  • 写回答

1条回答 默认 最新

  • duanmin0941 2017-01-10 14:08
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟