1

假设我有这个脚本:

http://www.example.com/profile.php?uid=12345

在这个脚本中,有一个由 sendmessage.php 处理的发送消息功能。sendmessage.php 实际所做的是将某些内容插入数据库,然后重定向回用户开始的位置(profile.php?uid=12345)。

我尝试使用标头(“位置:profile.php”)。

但是由于 profile.php 在 URL 中需要 ?uid=12345 [并且它是动态的],因此我的重定向计划不起作用。

如何“传递”当前 URL。在我的情况下,使用 GET 方法将 'profile.php?uid=12345' 发送到 sendmessage.php。然后,一旦 sendmessage.php 完成处理,将用户返回到他们自己的屏幕 (profile.php?uid=12345)?

提前致谢。

4

6 回答 6

2

尝试这个:

header("Location: " . $_SERVER['HTTP_REFERER']);

来自 PHP.net 的注释:

The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

于 2012-08-06T16:57:38.343 回答
1

如果您profile.php?uid=12345使用表单向其发送数据,sendmessage.php 那么您可以有一个隐藏字段,例如 -

<input type="hidden" name="redirect_to" value="<?=$_SERVER['REQUEST_URI']?>">

然后在sendmessage.php你可以使用header("Location: ".$_REQUEST['redirect_to']);'

于 2012-08-06T17:14:02.303 回答
0

一个技巧是在链接本身中填充信息,这样当您转到不同的页面时,这些参数仍然存在......如果您愿意,您可以使用一个参数作为以前的 url。

于 2012-08-06T16:57:48.037 回答
0

您可以使用 GET 和一些 javascript。使用这种方法可以避免使用 PHP 的 header 函数的问题。

echo "<script type=\"text/javascript\"> window.location = \"profile.php?uid=" . $_GET["uid"] .    "\"</script>";

确保检查“uid”是否实际通过。您还可以在用户首次登录时将 uid 存储在会话变量中,然后在我的示例中使用它,从而避免使用 GET。无论哪种方式都有效。

于 2012-08-06T19:42:02.270 回答
0

另一种选择是确保将 uid 作为表单提交数据的一部分发送。

然后您可以使用传入的值重建 URL:

header("位置:profile.php?uid=" . $_GET["uid"]);

或者,将用户 ID 存储在他们的会话中。这样,它总是在 $_SESSION["uid"] 中可用,并且您不必记住在表单中创建一个无关紧要的字段。

于 2012-08-06T17:10:44.620 回答
0

您应该将变量传递给 sendmessage.php

sendmessage.php?uid=<?php echo (isset($_GET['uid']) ? (INT)$_GET['uid'] : 'default') ?>

然后在 sendmessage.php 上。使用类似的东西

header("Location: profile.php?uid=" . (isset($_GET['uid']) ? (INT)$_GET['uid'] : 'default') ); 
于 2012-08-06T17:12:13.267 回答