duanlun2827 2016-06-06 21:17
浏览 61
已采纳

PHP即使在页面重新加载后也保持变量范围

The website generates the random number from 1 to 100 when accessing the first page(page1.php). And the user will guess the number.

The first page contains - a text box for accepting a number from the user, and a submit button.

The second page(page2.php) will be returned to the user if the guess number is too high or too low. And the page shows a message telling the user "Too High" or "Too Low". The page also contains a button(retry button) that allows the user to go back to the first page(page1.php) to re-enter a new number a button that allows the user to quit the game.

The third page(page3.php) is returned to the user if the guess is correct. The page displays "Correct", the random number, and the count of tries.

And I have this index.php which is heart for all the pages. And here is the code.

index.php

    <?php

$name = '';
$inputnumber = '';
$random = 33; //this is just an assumption to keep it simple
$message = '';
$guesscount = '';
if (isset($_POST['action'])) {
    $action = $_POST['action'];
}

if ($action === 'guess') {
    $guesscount = $_POST['$guesscount'];
    $inputnumber = $_POST['$inputnumber'];
    if ($inputnumber == $random) {
        $message = "Correct!";
        include 'page3.php';
    }
    if ($inputnumber > $random) {
        $message = "Too High";
        include 'page2.php';
    }
    if ($inputnumber < $random) {
        $message = "Too Low";
        include 'page2.php';
    }
}

if ($action === 'retry') {
    include 'page1.php';
}

page1.php

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Number Guess</title>
    </head>
    <body>
        <h1>Number Guess</h1>
        <form name="myForm" action="index.php" method="post" >
            Number Guess: <input type="text" name="$inputnumber" value="<?php if(isset($inputnumber)==1){
echo $inputnumber;}else echo ""; ?>" /><br>

            <input type="submit" name="action" value="guess" />
            <hr>
            Guess Count: <?php echo $guesscount; ?>
        </form>
    </body>
</html>

page2.php

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Number Guess</title>
    </head>
    <body>
        <h1>Number Guess</h1>
        <form name="myForm" action="index.php" method="post" >
            Message: <?php echo $message; ?>
            <input type="hidden" name="$guesscount" value="<?php echo $guesscount;?>"/><br>
           <input type="submit" name="action" value="retry" />

            <hr>
           Guess Count: <?php echo $guesscount;?>
        </form>
    </body>
</html>

page3.php

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Number Guess</title>
    </head>
    <body>
        <h1>Number Guess</h1>
        <form name="myForm" action="index.php" method="post" >
            Message: <?php echo $message; ?>
            Number of Tries: <?php echo $guesscount; ?>
           <input type="submit" name="action" value="ok" />

        </form>
    </body>
</html>

page1.php is the page to load first.

Challenge I have faced is, I couldn't keep the $guesscount stable always. It keeps resetting on me. I have tried session but couldn't resolve it.Please help resolving it. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongtanxi5676756 2016-06-06 21:33
    关注

    I don't know why but my gut feeling tells me that the reason why the session is not working for you on other pages is because you do not initiate it ??

    So what you have to do is:

    index.php

    <?php
    session_start();
    $_SESSION['myVariable'] = 'myVariable';
    ?>
    

    page1.php

    <?php
    session_start();
    $mySessionVar = $_SESSION['myVariable'];
    var_dump($mySessionVar); // <- this should print myVariable
    ?>
    

    You may get an error saying that $_SESSION is null or not set and to prevent that you can just enclose $_SESSION inside and isset method

    if(isset($_SESSION['myVariable']) && $_SESSION['myVariable'] != null) {
      $mySessionVar = $_SESSION['myVariable'[;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示