dtvnnhh8992 2016-04-02 19:30
浏览 36
已采纳

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.

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();
?>

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];
 ?>

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.

Thanks a looot

  • 写回答

1条回答 默认 最新

  • dskld5423 2016-04-02 20:38
    关注

    This might be a good solution:

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

    in the insert.php:

    $secQ = $_SESSION['secQ'];
    

    in the questions.php:

    $_SESSION['secQ'] = $secQ;
    

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败