-2

I am fetching random questions with options from database for online exam system developed in core php with help of following code. But I am facing following issues :

1) User must not allowed to refresh the page anyway[through keyboard or mouse].

2) If user opens the same url in tab in the same browser, there should be an alert, saying you are already logged in.

let me know which will be best solution in core php for the same.

Thanks

The Code is :

<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>
  • `PHP` is server-side, there's (probably) no way to stop a user from refreshing a page using PHP. Why shouldn't a user refresh? – brombeer Oct 29 '18 at 08:28
  • If User refresh page then, timer will get reset and random query again get executed.. – Nikhil Bhalerao Oct 29 '18 at 08:41
  • sorry but no you can't prevent the user to refresh the page, every browser, even if the user can't use F5 or whatever key that is, there's still the refresh button in the browser, you can't block it though, sorry – Kevin Oct 29 '18 at 08:43
  • 1
    an alternative way is to save the current session of the user if the browser is refreshed – Kevin Oct 29 '18 at 08:44
  • Might be easier to just store the timer and the random questions (per user) on your server – brombeer Oct 29 '18 at 08:44

1 Answers1

-1

To answer your questions.

1) User must not allowed to refresh the page anyway[through keyboard or mouse].

as kerbholz says - "You can't completely stop the user from reloading" for it is more or less impossible to determine the event of the client using PHP. instead you can achieve this by using the jquery. ready more... Prevent any form of page refresh using jQuery/Javascript

2) If user opens the same url in tab in the same browser, there should be an alert, saying you are already logged in.

achieving this you may use session variable to identify your session login. php login session login variables

Julius Limson
  • 526
  • 9
  • 29
  • "_let me know which will be best solution in core php for the same_" OP wants to do the "not refresh" thingy in PHP though. Also, from the accepted answer from that page: "_You can't completely stop the user from reloading_" – brombeer Oct 29 '18 at 08:43
  • @kerbholz you are correct. you can't completely stop the user from reloading a page from the server side. instead i'm giving alternative solution for the OP. – Julius Limson Oct 29 '18 at 08:52