PHP如何通过表单提交冻结随机数

PHP如何通过表单提交冻结随机数,php,Php,我想要一个程序来生成一个随机数,打印出那个数字,并在用户输入表单字段时保持相同的数字 现在,下面的代码总是刷新随机数生成 我不知道什么样的环境代码可以保存初始值,这样它就不会改变 $x = rand(5,50); echo $x."<br><br>"; echo "Enter the number you see above:"; function form1 () { function test_input_1($data) { $data

我想要一个程序来生成一个随机数,打印出那个数字,并在用户输入表单字段时保持相同的数字

现在,下面的代码总是刷新随机数生成

我不知道什么样的环境代码可以保存初始值,这样它就不会改变

$x = rand(5,50);

echo $x."<br><br>";

echo "Enter the number you see above:";

function form1 () {
    function test_input_1($data) {
       $data = trim($data);
       $data = stripslashes($data);
       $data = htmlspecialchars($data);
       return $data;
    }

    ?>

    <form id="question1" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
        <input type="text" name="user_input1" value="<?php if(isset($_POST['user_input1'])) { echo htmlentities ($_POST['user_input1']); }?>"/><br>
        <input type="submit" name="user_input1Submit" style="position: absolute; left: -9999px"/>
    </form>

    <?php

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if ($_POST['user_input1'] !="") {
            $user_input1 = test_input_1($_POST["user_input1"]);
            // more variables here, per line -- and add them to the ="" above.

            return $user_input1;
        }
    } 
}

form1();
$x=兰特(5,50);
echo$x.“

”; echo“输入您在上面看到的数字:”; 函数form1(){ 功能测试输入1($data){ $data=修剪($data); $data=条带斜杠($data); $data=htmlspecialchars($data); 返回$data; } ?> 当然,这是假设
user\u input1
是随机数的字段

<?php
    function getRandom(){
        return rand(5,50);
    }
//function form1 () {
    function cleanInput($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

    $x = getRandom();
    echo $x."<br><br>";
    echo "Enter the number you see above:";
?>

<form id="question1" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
    <input type="text" name="user_input1" value="<?php echo isset($_POST['user_input1']) ? cleanInput($_POST['user_input1'] ) : ''; ?>"/><br>
    <input type="submit" name="user_input1Submit" style="position: absolute; left: -9999px"/>
</form>

<?php
    /*? what's this for - but I would change
     the first if in unneeded if isset($_POST['user_input1']))
     then obviously it should be request method
     in any case it returns to oblivion below here.
     besides shouldn't it be above the form, just as easy to call in the form echo $_POST part, no?
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if ($_POST['user_input1'] !="") {
            $user_input1 = test_input_1($_POST["user_input1"]);
            // more variables here, per line -- and add them to the ="" above.

            return $user_input1;
        }
    } 
}
form1(); <-- oblivion as it's not setting anything.
*/
?>

当然,这是假设
user\u input1
是随机数的字段

<?php
    function getRandom(){
        return rand(5,50);
    }
//function form1 () {
    function cleanInput($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

    $x = getRandom();
    echo $x."<br><br>";
    echo "Enter the number you see above:";
?>

<form id="question1" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
    <input type="text" name="user_input1" value="<?php echo isset($_POST['user_input1']) ? cleanInput($_POST['user_input1'] ) : ''; ?>"/><br>
    <input type="submit" name="user_input1Submit" style="position: absolute; left: -9999px"/>
</form>

<?php
    /*? what's this for - but I would change
     the first if in unneeded if isset($_POST['user_input1']))
     then obviously it should be request method
     in any case it returns to oblivion below here.
     besides shouldn't it be above the form, just as easy to call in the form echo $_POST part, no?
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if ($_POST['user_input1'] !="") {
            $user_input1 = test_input_1($_POST["user_input1"]);
            // more variables here, per line -- and add them to the ="" above.

            return $user_input1;
        }
    } 
}
form1(); <-- oblivion as it's not setting anything.
*/
?>


您可能知道,HTTP是无状态协议。创建Cookie和会话是为了跟踪用户(或请求)。您必须在代码中使用会话来消除随机数。我编写了一个简单的代码,并将其添加到示例的开头

    <?php
session_start(); // start the session
if(isset($_SESSION['random'])) {
    $x = $_SESSION['random'];
} else {
    $x = rand(5,50);
    $_SESSION['random'] = $x;
}



echo $x."<br><br>";

echo "Enter the number you see above:";

function form1 () {
    function test_input_1($data) {
       $data = trim($data);
       $data = stripslashes($data);
       $data = htmlspecialchars($data);
       return $data;
    }

    ?>

    <form id="question1" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
        <input type="text" name="user_input1" value="<?php if(isset($_POST['user_input1'])) { echo htmlentities ($_POST['user_input1']); }?>"/><br>
        <input type="submit" name="user_input1Submit" style="position: absolute; left: -9999px"/>
    </form>

    <?php

    if ($_SERVER["REQUEST_METHOD"] == "POST") {

        if ($_POST['user_input1'] !="") {

            $user_input1 = test_input_1($_POST["user_input1"]);
            // more variables here, per line -- and add them to the ="" above.
            return $user_input1;
        }
    } 
}

form1();


您可能知道,HTTP是无状态协议。创建Cookie和会话是为了跟踪用户(或请求)。您必须在代码中使用会话来消除随机数。我编写了一个简单的代码,并将其添加到示例的开头

    <?php
session_start(); // start the session
if(isset($_SESSION['random'])) {
    $x = $_SESSION['random'];
} else {
    $x = rand(5,50);
    $_SESSION['random'] = $x;
}



echo $x."<br><br>";

echo "Enter the number you see above:";

function form1 () {
    function test_input_1($data) {
       $data = trim($data);
       $data = stripslashes($data);
       $data = htmlspecialchars($data);
       return $data;
    }

    ?>

    <form id="question1" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
        <input type="text" name="user_input1" value="<?php if(isset($_POST['user_input1'])) { echo htmlentities ($_POST['user_input1']); }?>"/><br>
        <input type="submit" name="user_input1Submit" style="position: absolute; left: -9999px"/>
    </form>

    <?php

    if ($_SERVER["REQUEST_METHOD"] == "POST") {

        if ($_POST['user_input1'] !="") {

            $user_input1 = test_input_1($_POST["user_input1"]);
            // more variables here, per line -- and add them to the ="" above.
            return $user_input1;
        }
    } 
}

form1();


您可以将该值存储在隐藏的输入字段或会话变量中。这是用于验证码sorta身份验证的,对吗?…如果随机性类似于验证码,那么会话存储更适合它们,因为PHP何时允许嵌套函数?@PHPglue-@PHPglue的PHP版本!!您可以存储该值在隐藏的输入字段或会话变量中。这是用于验证码sorta身份验证的,对吗?…如果随机性类似于验证码,那么会话存储更适合它们,因为PHP什么时候允许嵌套函数?@PHPglue-@PHPglue of the PHP advent!!解决方案与我给定的代码完全匹配(这是较长代码的代理缩写——您的解决方案与较长代码匹配)…非常简洁,解释得很好。谢谢!我刚开始使用stackoverflow,所以我的投票在我获得更多分数之前不会显示。冻结非常牢固,我可以从浏览器窗口退出,从浏览器退出,然后重新启动我的计算机--浏览器仍然记得冻结的值。我希望它们能够更改从一个浏览器会话到另一个浏览器会话,甚至在刷新时。解决方案与我给定的代码完全匹配(这是较长代码的代理缩写,而您的解决方案与较长代码匹配)…非常简洁,解释得很好。谢谢!我刚开始使用stackoverflow,所以我的投票在我获得更多分数之前不会显示。冻结非常牢固,我可以从浏览器窗口退出,从浏览器退出,然后重新启动我的计算机--浏览器仍然记得冻结的值。我希望它们能够更改从一个浏览器会话切换到另一个浏览器会话,甚至在刷新时。