【问题标题】:PHP Redirect with GET method使用 GET 方法进行 PHP 重定向
【发布时间】:2015-07-02 09:16:10
【问题描述】:

当人们对我的网页发表评论时,它会首先验证你是否已经登录。

如果没有,那么你必须先登录,我在这里尝试做的是,在你登录后。当你尝试发表评论时,你将重定向到最后一页

所以,我尝试使用 PHP header("Location:"); 使用 GET 方法重定向页面

这是我的代码:

HTML:

<form method="POST" action="addcomment.php">
    <textarea class="commentarea" name="comment" placeholder="your comment"></textarea>
    <input type="submit" name="submitcomment" value="Comment" />
</form>

addcomment.php:

header("Location:../loginfirst.php?location=".$_SERVER['HTTP_REFERER']);

它会发送到 loginfirst.php 页面,并且 url 看起来像这样:

http://localhost/ayojo/loginfirst.php?location=http://localhost/ayojo/product.php?p=test

之后,我在 loginfirst.php 上尝试此代码:

$_GET['location'] = $test;

    header('Location:'.$test);

最后,我尝试登录然后成功。但是,php 标头将我重定向到 loginfirst.php,而不是 $_GET['location']

我的代码有什么问题?

【问题讨论】:

标签: php redirect


【解决方案1】:

您不应该将变量 $test 声明为 $_GET 吗? 所以在 loginfirst.php 中:

$test = $_GET['location'];

你现在反其道而行之。

【讨论】:

  • 艾肯,感谢您的回答。我试试你的方法还是不行
【解决方案2】:

在设置标头时使用 urlencode 并将 $_GET['location'] 的值分配给 $test。

添加评论.php:

header("位置:loginfirst.php?location=".urlencode($_SERVER['HTTP_REFERER']));

loginfirst.php:

$test = $_GET['location']; header('位置:'.$test);

【讨论】:

  • 感谢 dhi_m 的回答,它仍然无法正常工作。登录后,我将重定向到 loginfirst.php 而不是最后一个评论页面。
  • 您是否在其他页面中查看会话?如果是,请确保已设置会话。
猜你喜欢
  • 2011-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-04
  • 2011-07-31
  • 2012-03-10
  • 1970-01-01
相关资源
最近更新 更多