php 如何通过php标头重定向传递在GET字符串中收到的变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11803343/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:08:52  来源:igfitidea点击:

How to pass variables received in GET string through a php header redirect?

phpvariablesheaderget

提问by user1322707

I'm receiving values in a GET string from Aweber upon user's submission of a form. I take the variables they send and submit them to a SMS gateway to notify a 3rd party of the submission by text message.

在用户提交表单后,我正在从 Aweber 接收 GET 字符串中的值。我将他们发送的变量提交给 SMS 网关,以通过短信通知第 3 方提交。

Here's my problem. I need to redirect the page that performs the outgoing SMS commands in a php header to another page that finally displays the GET variables sent from Aweber.

这是我的问题。我需要将在 php 标头中执行传出 SMS 命令的页面重定向到另一个页面,该页面最终显示从 Aweber 发送的 GET 变量。

I can retrieve the variables and their values in the first page. How do I pass them to the second page?

我可以在第一页中检索变量及其值。我如何将它们传递到第二页?

Here is the code I'm using on the first page (sms.php) to collect the variables sent by Aweber:

这是我在第一页 (sms.php) 上用来收集 Aweber 发送的变量的代码:

   $fname   = $_GET['name'];
   $femail  = $_GET['email'];
   $fphone  = $_GET['telephone'];
   ....etc

   header('Location: confirmed.php');
   exit;

回答by

First convert the $_GETHTTP variable into a query string using

首先使用以下命令将$_GETHTTP 变量转换为查询字符串

$query = http_build_query($_GET);

Then append the query string variable to your redirect header

然后将查询字符串变量附加到您的重定向标头

header('location: domain.com'."?".$query);

Done.

完毕。

回答by Kalpesh

session_start();
$_SESSION['fname']   = $_GET['name'];
$_SESSION['femail']  = $_GET['email'];
$_SESSION['fphone']  = $_GET['telephone'];
....etc

header('Location: confirmed.php');

and get it on the next page like:

并在下一页获取它,例如:

session_start();
$fname   = $_SESSION['fname'];
$femail  = $_SESSION['femail'];
$fphone  = $_SESSION['fphone'];

....etc

....等等

回答by Djerro Neth

You don't need to store them in a session, you can easily pass them with your location header:

您不需要将它们存储在会话中,您可以轻松地将它们与您的位置标头一起传递:

$fname   = $_GET['name'];
$femail  = $_GET['email'];
$fphone  = $_GET['telephone'];
//now a header with these var's:
header("Location: confirmed.php?name=".$fname."&email=".$femail."&telephone=".$fphone);

In confirmed.php you can get these variables with $_GET method.

在confirmed.php 中,您可以使用$_GET 方法获取这些变量。

回答by Yan Berk

Store them in the session:

将它们存储在会话中

 $_SESSION['fname'] = $_GET['name'];

Use session_startat the beginning of each file.

session_start在每个文件的开头使用。

回答by david.ee

Please for anyone reading this in future, use sessions for this kind of variable value transfer because if you rely mostly on adding variable to header then if the user in still on that form and carries out an action that changes the value of the header then your own variable value changes since it depends on the header......simply put, USE SESSIONS.

请为将来阅读本文的任何人使用会话进行这种变量值传输,因为如果您主要依赖于向标题添加变量,那么如果用户仍在该表单上并执行更改标题值的操作,那么您的自己的变量值会发生变化,因为它取决于标题......简单地说,使用 SESSIONS。

回答by James Anderson Jr.

Try this. It worked perfectly for me.

尝试这个。它非常适合我。

if ($_GET)
{
    $query = str_replace("%3D", "=", str_replace("%26", "&", strval(urlencode(http_build_query($_GET)))));

    header('location: https://www.example.com'.'?'.$query);
}
else
{
    header('location: https://www.example.com');
};

回答by Jaro

The best you can do is put all your POST variables to a session like this:

你能做的最好的事情就是把你所有的 POST 变量放到一个像这样的会话中:

On page1.php put:

在 page1.php 上放:

//Start the session
session_start();
//Dump your POST variables
$_SESSION['post-data'] = $_POST;

And on page2.php put: (If on page1.php we use a normal POST form submit with form action="page2.php")

在 page2.php 上放:(如果在 page1.php 上我们使用普通的 POST 表单提交表单action="page2.php

//Start the session
session_start();
//Access your POST variables
foreach ($_POST as $key => $value) {
    ${$key} = $value;
    $_SESSION[$key] = $value;
}
//Unset the useless session variable
unset($_SESSION['post-data']);

Or on page2.php put: (If on page1.php we use a self submit with form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>and then use a header("Location: page2.php");to move to the page2.php and pass our POST variables via a session)

或者在 page2.php 上放置:(如果在 page1.php 上我们使用带有表单的自我提交,action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>然后使用 aheader("Location: page2.php");移动到 page2.php 并通过会话传递我们的 POST 变量

//Start the session
session_start();
//Access your POST variables
$_POST = $_SESSION['post-data'];
foreach ($_POST as $key => $value) {
    ${$key} = $value;
    $_SESSION[$key] = $value;
}
unset($_SESSION['post-data']);

I literally spent hours figuring that out because all the forums put it wrong or incomplete.

我真的花了好几个小时才弄明白,因为所有的论坛都把它说错了或不完整。

Now it's as easy as just calling the variables you passed from the page1.php like this for example: <b>Points: </b><?php echo $points; ?>and that's it!!

现在就像调用你从 page1.php 传递的变量一样简单,例如:<b>Points: </b><?php echo $points; ?>就是这样!

When situating the header('Location: page2.php');in a ifcondition, etc. make sure that it will be in the first PHP script of the page and above any HTML output.

header('Location: page2.php');放置在if条件中时,请确保它位于页面的第一个 PHP 脚本中,并且位于任何 HTML 输出的上方。

回答by Adnan

This works use this sentex

这个作品使用这个sentex

header('location:member_dashboard.php?id='.$id);

header('location:member_dashboard.php?id='.$id);