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.

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

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像