简体   繁体   English

如何阻止某人返回上一页?

[英]How to stop someone from going back to previous page?

I am working on a my site to allow users to take test so they can see how much they know of a particular subject.我正在我的网站上工作,以允许用户进行测试,以便他们可以了解他们对特定主题的了解程度。 I am running into a little problem though.不过我遇到了一个小问题。 Once a user submits the test for grading, how do I prevent them from going back to the test page?用户提交评分测试后,如何防止他们返回测试页面? I am on a Mac with Safari running and when I click the back button in my web browser after I submit the test it leaves all of the answers I answered filled out.我在运行 Safari 的 Mac 上,当我在提交测试后单击 Web 浏览器中的后退按钮时,它会留下我回答的所有答案。 I want it do this: When a user submits a test and they click the back button in their web browser it redirects them to the main test page.我希望它这样做:当用户提交测试并单击其 Web 浏览器中的后退按钮时,它会将他们重定向到主测试页面。

I am using PHP and MYSQL.我正在使用 PHP 和 MYSQL。 I even have the test pages setup so that the user must come from a certain url (I am using HTTP_REFERER) and I have tried other stuff such as sessions but I cannot seem to figure this out.我什至设置了测试页面,以便用户必须来自某个网​​址(我使用的是 HTTP_REFERER),并且我尝试了其他内容,例如会话,但我似乎无法弄清楚这一点。 Any help is greatly appreciated.任何帮助是极大的赞赏。

You don't stop them.你不阻止他们。

Instead change your application so that it still works even if they go back.而是更改您的应用程序,使其即使返回也仍然有效。 You can embed a unique number in a hidden field on the page and if they resubmit the same test twice you can detect it and display an appropriate error message.您可以在页面的隐藏字段中嵌入唯一编号,如果他们重新提交相同的测试两次,您可以检测到它并显示相应的错误消息。 You should also think about what should happen if they modify the unique number.您还应该考虑如果他们修改唯一编号会发生什么。

If you don't want people to post different answers once they have already answered, all you have to do is check, in the script that accepts the test for grading, that the user has never submitted the test before.如果您不希望人们在他们已经回答后发布不同的答案,您所要做的就是在接受评分测试的脚本中检查用户之前从未提交过测试。 If you don't, a clever student will always be able to to circumvent your protection by sending an appropriate request directly to that script.如果您不这样做,聪明的学生将始终能够通过直接向该脚本发送适当的请求来绕过您的保护。

If you don't want people to see previous answers (for instance, if you have two people grade their tests on the same computer), consider using AJAX on the test page to submit the answers and then erase them from the fields.如果您不希望人们看到以前的答案(例如,如果您有两个人在同一台计算机上为他们的测试评分),请考虑在测试页面上使用 AJAX 提交答案,然后将其从字段中删除。 This way, most browsers will not remember the answers and the back button will not un-erase data that was erased by JavaScript.这样,大多数浏览器不会记住答案,后退按钮也不会清除被 JavaScript 擦除的数据。

At the top of the grade page, put the following:在成绩页面的顶部,放置以下内容:

session_start();
$_SESSION['testcomplete'] = 'yes';

Then at the top of each page of the test, put this:然后在测试的每个页面的顶部,放置:

session_start()
if ($_SESSION['testcomplete'] == 'yes') {
    header("Location:cheater.php");
}

You could simulate there being no page to go back to.您可以模拟没有可返回的页面。 From one page, generate each test page using jQuery, and provide no way to go back, only forward.从一个页面开始,使用jQuery生成每个测试页面,并提供无法返回,只能前进的方式。 The back button would take them to the page before they ever launched the test, and you could allow them to launch the test again and generate the right part where they should be.在他们启动测试之前,后退按钮会将他们带到页面,您可以允许他们再次启动测试并生成正确的部分。 This would be pretty easy, if you haven't gone too far in development the current way.如果您目前的开发方式还没有走得太远,这将非常容易。

You could run javascript that clears out all the answers.您可以运行 javascript 清除所有答案。 You might also just allow one submission so that subsequent submissions don't get processed.您也可能只允许一次提交,以便不会处理后续提交。 HTTP_REFERER is usually sent, but can be spoofed and forged by an altered browser. HTTP_REFERER 通常被发送,但可以被更改的浏览器欺骗和伪造。

On the top of the script POST-ing the answers, do a check whether you have the test results in the database for the current user for this test.在 POST-ing the answers 脚本的顶部,检查数据库中是否有当前用户的测试结果。 If you do, redirect to results.如果这样做,请重定向到结果。

if(get_test_results($user)){
   $test_url = get_test_url($user);
   header( "Location: $test_url" ) ;
}

Disabling the back button is not a good idea.禁用后退按钮不是一个好主意。

I was facing a similar problem making an online examination myself我在自己进行在线考试时遇到了类似的问题

what I did is我所做的是

  • I provided a session variable such that if the user pastes the previous page's URL in the address bar then on loading the page the page is automatically forwards to the next desired page.我提供了一个会话变量,这样如果用户将上一页的 URL 粘贴到地址栏中,则在加载页面时,该页面会自动转发到下一个所需的页面。 Whether the page whose URL was mentioned is the being visited the first time or being revisited is determined by the value of the session variable被提及的页面是第一次访问还是再次访问由会话变量的值决定

  • If the user instead of loading the page does a go back via the browser button the it automatically redirects to the next page in history as :如果用户不是加载页面而是通过浏览器按钮返回,它会自动重定向到历史记录中的下一页:

javascript:window.history.forward(1); javascript:window.history.forward(1);

Hope this helps :)希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM