Javascript 如何防止在浏览器的不同选项卡中访问刷新和相同URL:

Javascript 如何防止在浏览器的不同选项卡中访问刷新和相同URL:,javascript,php,jquery,Javascript,Php,Jquery,我在以下代码的帮助下,从核心php开发的在线考试系统的数据库中获取带有选项的随机问题。但我面临以下问题: 1)无论如何都不能允许用户[通过键盘或鼠标]刷新页面。 2)如果用户在同一浏览器的选项卡中打开相同的url,则会出现一个警报,表明您已登录。 让我知道哪一个将是最好的解决方案在核心php的相同 谢谢 代码是: <form action="submit-assessment.php" method="post"> <?p

我在以下代码的帮助下,从核心php开发的在线考试系统的数据库中获取带有选项的随机问题。但我面临以下问题:

1)无论如何都不能允许用户[通过键盘或鼠标]刷新页面。

2)如果用户在同一浏览器的选项卡中打开相同的url,则会出现一个警报,表明您已登录。

让我知道哪一个将是最好的解决方案在核心php的相同

谢谢

代码是:

<form action="submit-assessment.php" method="post">
                            <?php

               $generated_q = mysqli_query($conn, "SELECT * FROM `tbl_question_paper` WHERE `exam_id`='$exam_id' ORDER BY RAND() LIMIT $no_ques");
                            while ($row_generated_q = mysqli_fetch_array($generated_q)) {
                                $question_results[] = $row_generated_q;
                            }
                            foreach ($question_results as $question) {
                                  $ques_set = $question['setid'];
                                  $ques_question_type = $question['question_type'];
                                  $ques_level = $question['level'];
                                  $ques_count_number = $question['count_number'];


                               $select_questions1 = mysqli_query($conn, "SELECT * FROM `tbl_questions` WHERE `set_id`='$ques_set' AND `question_type`='$ques_question_type' AND  `level`='$ques_level' AND `invalid_status_admin`='0' ORDER BY RAND() LIMIT $ques_count_number");


                               while($quest_results = mysqli_fetch_array($select_questions1)){

                       $quest_results1[] = $quest_results;


               } }

                             $i = 0;
                             ?>
                                <input type="hidden" name="exam_id" value="<?php echo $exam_id; ?>" id="exam_id" checked>


                                <?php

                                foreach ($quest_results1 as $row_quest) {
                                    $i++;

                                    $question_exam = $row_quest['question'];
                                    if($row_quest['question_image']!='')
                                    {
                                        $question_image='<img src="../../uploadImage/question/'.$row_quest['question_image'].'" style="width: 30%;height: auto;">';
                                    }
                                    else
                                    {
                                        $question_image='';
                                    }


                                        $optiona = $row_quest['option1'];
                                        if($row_quest['option1_image']!='')
                                    {

                                        $option1_image='<img src="../../uploadImage/question/'.$row_quest['option1_image'].'" style="width: 30%;height: auto;">';
                                    }
                                    else
                                    {
                                        $option1_image='';
                                    }
                                        $optionb = $row_quest['option2'];
                                        if($row_quest['option2_image']!='')
                                    {
                                        $option2_image='<img src="../../uploadImage/question/'.$row_quest['option2_image'].'"  style="width: 30%;height: auto;">';
                                    }
                                    else
                                    {
                                        $option2_image='';
                                    }
                                        $optionc = $row_quest['option3'];
                                        if($row_quest['option3_image']!='')
                                    {
                                        $option3_image='<img src="../../uploadImage/question/'.$row_quest['option3_image'].'" style="width: 30%;height: auto;" >';
                                    }
                                    else
                                    {
                                        $option3_image='';
                                    }
                                        $optiond = $row_quest['option4'];
                                        if($row_quest['option4_image']!='')
                                    {
                                        $option4_image='<img src="../../uploadImage/question/'.$row_quest['option4_image'].'" style="width: 30%;height: auto;">';
                                    }
                                    else
                                    {
                                        $option4_image='';
                                    }


                                        echo '<div id="' . $row_quest['exam_id'] . '_' . $i . '"  class="display_question" name="'.$i.'">';
                                        //$groupInfo_default = Exams::groupInfo($row_quest['group_id'], "default_group");

                                        //if ($groupInfo_default == "no") {
                                        //    echo '<h4>Тема: ' . Exams::groupInfo($row_quest['group_id'], "name") . '</h4>';
                                        //}
                                        echo '
<div class="well">
<h4>Exam. ' . $i . ' ' . $question_exam . '</h4>'.$question_image.'
<table width="100%" border="0" class="answeers">
<input type="text" name="que[]" value="' . $row_quest['question_id'] . '" data-id="'.$row_quest['question_id'].'" id="'.$row_quest['question_id'].'" checked>';

                                       // if ($row_quest['number_options'] == 2) {
                                            echo '
<tr height="40"><td>(A) <input name="' . $row_quest['question_id'] . '"  value="1" type="radio"> ' . $optiona.$option1_image. ' </td></tr>
<tr height="40"><td>(B) <input name="' . $row_quest['question_id'] . '" value="2" type="radio"> ' . $optionb .$option2_image. '</td></tr>

<tr height="40"><td>(C) <input name="' . $row_quest['question_id'] . '"  value="3" type="radio" id=""> ' . $optionc.$option3_image. '</td></tr>
';
                                            echo '
<tr height="40"><td>(D) <input name="' . $row_quest['question_id'] . '"  value="4" type="radio" id=""> ' . $optiond.$option4_image . '</td></tr>';
                                       // }
                                        echo '
</table>
</div>
</div>';

                                }
                                ?>
                               <div class="col-md-12 padd  down-buttons">
                                    <div class="col-md-6 padd">
                                        <div class="btn btn-info down-bt"  id="prev">previous</div>
                                        <div class="btn btn-info down-bt" id="mnext">Mark & next</div>
                                        <div class="btn btn-info down-bt" id="next">next</div>
                                        <div class="btn btn-info down-bt" id="invalid">Invalid</div>
                                    </div>
                                    <div class="col-md-6" style="margin:0px;"> 
                                        <button type="submit"  style="float:right;margin-left:5px;" id="finish" class="btn btn-success down-bt pull-right" name="finish"
                                                onclick="finished(); return confirm('Are You Sure Finish Exam ?'); "/>
                                        <i class="fa fa-check"></i> Finish the exam
                                        </button>
                                        <div class="btn btn-info down-bt" style="float:right;" id="clearAnswer">Clear answer
                                        </div>
                                    </div>
                                </div>
                             <?php $finish_time = time();?>
                              <input type="text" id="sesstime" name="sesstime" value="">
                             <input type="hidden" id="fini" name="fini" value="">
                            </form>


回答你的问题

1)无论如何都不能允许用户[通过键盘或鼠标]刷新页面。

正如kerbholz所说,“您不能完全阻止用户重新加载”,因为使用PHP确定客户端的事件或多或少是不可能的。相反,您可以通过使用jquery来实现这一点。准备好更多

2)如果用户在同一浏览器的选项卡中打开相同的url,则会出现一个警报,表明您已登录。


为此,您可以使用会话变量来标识您的会话登录

PHP
是服务器端的,可能无法阻止用户使用PHP刷新页面。为什么用户不应该刷新?如果用户刷新页面,计时器将重置并再次执行随机查询..对不起,您不能阻止用户刷新页面,每个浏览器,即使用户不能使用F5或其他键,浏览器中仍然有刷新按钮,但您不能阻止它,sorryan的另一种方法是在浏览器刷新时保存用户的当前会话。只需将计时器和随机问题(每个用户)存储在服务器上可能会更容易“让我知道哪一个将是相同的核心php的最佳解决方案”OP想用php做“不刷新”的事情。另外,从该页面的公认答案来看:“你不能完全阻止用户重新加载”@kerbholz,你是对的。您无法完全阻止用户从服务器端重新加载页面。相反,我为OP提供了另一种解决方案。