13
votes

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.

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.

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:

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

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

8 Answers

16
votes
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

24
votes

First convert the $_GET HTTP variable into a query string using

$query = http_build_query($_GET);

Then append the query string variable to your redirect header

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

Done.

10
votes

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.

3
votes

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.

2
votes

Store them in the session:

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

Use session_start at the beginning of each file.

2
votes

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');
};
1
votes

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

On page1.php put:

//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")

//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)

//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!!

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

0
votes

This works use this sentex

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