2

As I try to retrieve the variable status from the $_REQUEST[] array I set in the first script (and then do a redirect), I see nothing but a warning Undefined index: status. Why is that ?

<?php
        $_REQUEST['status'] = "success";
        $rd_header = "location: action_script.php";
        header($rd_header);
?>

action script.php

<?php
echo "Unpacking the request variable : {$_REQUEST['status']}";
John Conde
  • 217,595
  • 99
  • 455
  • 496
saplingPro
  • 20,769
  • 53
  • 137
  • 195

3 Answers3

4

What you're looking for is sessions:

<?php
    session_start();
    $_SESSION['status'] = "success";
    $rd_header = "location: action_script.php";
    header($rd_header);
?>

<?php
    session_start();
    echo "Unpacking the request variable : {$_SESSION['status']}";

Note the addition of session_start() at the top of both pages. As you'll read in the link I posted this is required and must be on all pages you wish to use sessions.

John Conde
  • 217,595
  • 99
  • 455
  • 496
4

It is because your header() statement redirects the user to a brand new URL. Any $_GET or $_POST parameters are no longer there because we are no longer on the same page.

You have a few options.

1- Firstly you can use $_SESSION to persist data across page redirection.

session_start();
$_SESSIONJ['data'] = $data; 
// this variable is now held in the session and can be accessed as long as there is a valid session.

2- Append some get parameters to your URL when redirecting -

$rd_header = "location: action_script.php?param1=foo&param2=bar";
header($rd_header);
// now you'll have the parameter `param1` and `param2` once the user has been redirected.

For the second method, this documentation might be usefull. It is a method to create a query string from an array called http_build_query().

Lix
  • 47,311
  • 12
  • 103
  • 131
2

What you are looking for is probably sending a GET parameter:

$rd_header = "Location: action_script.php?status=success";
header($rd_header);

that can be retrieved in the action_script.php via:

$_GET['status'];

You don't really need sessions or cookies in this case but you have to consider the fact that a GET post can be easily edited by the user.

Shoe
  • 74,840
  • 36
  • 166
  • 272
  • 2
    +1 For mentioning the problem of "trusting" the data in a `$_GET` request. **Never trust those users!** ;) – Lix Mar 17 '13 at 16:52