PHP,随机序列与关键 [英] PHP, Random array with key

查看:147
本文介绍了PHP,随机序列与关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下阵列产生与一个关键的形式随机问题。

I have the below array which generates a random question with its key for a form.

我保存在我的数据库,供以后使用的关键,例如,密码恢复等...

I save the key in my database for later use, eg Password recovery etc...

问题是每一个问题是问这是保存在数据库中的关键时间大多没有一个是问的问题。

The problem is each time the question is asked the key which is saved in the database is mostly not the one for the asked question.

下面是我insert.php保存键(secQ):

Here is my insert.php for saving the key (secQ):

所有的连接和正常工作验证...

All the connections and validations which work correctly...

<?php
    include("questions.php");
    $dbh= $pdo->prepare("INSERT INTO users (secQ ) values (?)");
    $dbh->bindParam(1, $secQ );
    $dbh->execute();
?>

这是我questions.php:

And this is my questions.php:

<?php
    function secQ(){
        $questions = array();

        $questions[0] = "1";
        $questions[1] = "2";
        $questions[2] = "3";
        $questions[3] = "4";
        $questions[4] = "5";

        $rand_key = array_rand($questions, 1);
        return array($rand_key, $questions[$rand_key]);
     }
    $q = secQ();
    $secQ = $q[0];
    $question = $q[1];
 ?>

我想这是由于这样的事实,即前secQ被插入到数据库
还有就是包括(questions.php);它可能会再次生成不同的密钥比形式askedquestion随机等。

I guess this is due to the fact that before secQ is inserted into the database there is the include("questions.php"); which it may again generate a different key randomly other than the askedquestion in the form.

感谢looot

推荐答案

这可能是一个很好的解决方案:

This might be a good solution:

我们可以使用会话的PHP文件之间进行通信如下:

We can use sessions to communicate between the php files as follows:

在insert.php:

in the insert.php:

$secQ = $_SESSION['secQ'];

在questions.php:

in the questions.php:

$_SESSION['secQ'] = $secQ;

这个工作,但是我担心这种方法的安全性,因为我右边的数据插入到数据库之前开始为insert.php会话。

This works, but Im concerned about the security of this method as I start the session for the insert.php right before inserting the data into the database.

在这个任何评论都将有很大的帮助,谢谢。

Any Comments on this would help a lot, thank you.

这篇关于PHP,随机序列与关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆