简体   繁体   English

在php中获取上一页的URL

[英]To obtain the previous page's URL in php

i have got 5 php pages which are question papers (MCQ's). 我有5个PHP页面是问题文件(MCQ的)。

the user is provided with 1 of the papers...which he answers and submits...it then goes to AnsCheck.php ...in AnsCheck.php i need to understand from which page ie from which of the 5 papers the request was received so that i can proceed with the checking ...how do i obtain the page from where i received the request? 用户提供了1篇论文...他回答并提交......然后转到AnsCheck.php ...在AnsCheck.php中我需要了解从哪个页面,即5篇论文中的哪一篇请求收到以便我可以继续检查...如何从我收到请求的地方获取页面?

----1.php---- ---- ---- 1.PHP

<?php
(E_ALL & ~E_NOTICE);

session_start();

// is the one accessing this page logged in or not?
if (!isset($_SESSION['db_is_logged_in'])
   || $_SESSION['db_is_logged_in'] !== true) {

   // not logged in, move to login page
   header('Location: login.php');
   exit;
}

?>
<html>
<head>
<title>My Page</title>
</head>
<body>
<form name="1" action="/NewDir/AnsCheck.php" method="POST">
1.Name the owl of harry potter.
<div align="left"><br>
<input type="radio" name="paper1" value="op1">Mr Barnesr<br>
<input type="radio" name="paper1" value="op2" checked> Wighed<br>
<input type="radio" name="paper1" value="op3"> Hedwig<br>
<input type="radio" name="paper1" value="op4"> Muggles<br>
<input type="submit" name="submit" value="Go">
</div>
</form>
</body>
</html>

$_SERVER['HTTP_REFERER'] contains the referring page. $_SERVER['HTTP_REFERER']包含引用页面。 (And, yes, it is spelled wrong in PHP because it is spelled wrong in the actual HTTP spec. Go figure.) (并且,是的,它在PHP中拼写错误,因为它在实际的HTTP规范中拼写错误。转到图。)

However, whether or not that header is sent is sometimes an option in the browser which some users disable, and the really old browsers don't even support it at all, so depending on it can be problematic. 但是,无论是否发送该标题有时是某些用户禁用的浏览器中的选项,并且真正的旧浏览器根本不支持它,因此取决于它可能会有问题。

Your code will be more likely to work for more users if you simply add a hidden field to each of these 5 forms indicating which form it is. 如果您只是为这5个表单中的每个表单添加一个隐藏字段来指示它是哪个表单,那么您的代码将更有可能为更多用户工作。

You're already using sessions. 你已经在使用会话了。 I would use them again here: 我会在这里再次使用它们:

$_SESSION['last_question'] = 1;

You can then check this in AnsCheck. 然后,您可以在AnsCheck中进行检查。 Alternatively, you could put a hidden field into your form: 或者,您可以在表单中放入一个隐藏字段:

<input type="hidden" name="question" value="1">

And then check the value of this in AnsCheck with $_POST['question'] . 然后使用$_POST['question']检查AnsCheck中的值。

Both of these are more reliable than HTTP_REFERER , which is not supplied by all browsers. 这两者都比HTTP_REFERER更可靠, HTTP_REFERER并非由所有浏览器提供。

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

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