Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't I see the request variable I set in the first script?

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']}";
like image 583
saplingPro Avatar asked Nov 30 '25 08:11

saplingPro


2 Answers

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().

like image 146
Lix Avatar answered Dec 02 '25 22:12

Lix


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.

like image 20
John Conde Avatar answered Dec 02 '25 22:12

John Conde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!