php 在php中获取上一页的URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4004416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
To obtain the previous page's URL in php
提问by Vinod K
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 i.e 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>
回答by Matchu
$_SERVER['HTTP_REFERER']
contains the referring page. (And, yes, it is spelled wrong in PHP because it is spelled wrong in the actual HTTP spec. Go figure.)
$_SERVER['HTTP_REFERER']
包含引用页面。(而且,是的,它在 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 个表单中的每一个添加一个隐藏字段,指示它是哪个表单,您的代码将更有可能为更多用户工作。
回答by lonesomeday
You're already using sessions. I would use them again here:
您已经在使用会话。我会在这里再次使用它们:
$_SESSION['last_question'] = 1;
You can then check this in AnsCheck. Alternatively, you could put a hidden field into your form:
然后,您可以在 AnsCheck 中进行检查。或者,您可以在表单中添加一个隐藏字段:
<input type="hidden" name="question" value="1">
And then check the value of this in AnsCheck with $_POST['question']
.
然后在 AnsCheck 中检查 this 的值$_POST['question']
。
Both of these are more reliable than HTTP_REFERER
, which is not supplied by all browsers.
这两者都比 更可靠HTTP_REFERER
,并非所有浏览器都提供。