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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?