Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep all the POST information while redirecting in PHP?

Tags:

post

php

header('Location: ' . $uri);

This will miss all the $_POST information.

like image 592
compile-fan Avatar asked Mar 05 '11 13:03

compile-fan


2 Answers

Don't use $_SESSION as you have been suggested. Session data is shared with all other pages, including the ones open in other tabs. You may get unpredictable behaviour if you use the same trick in multiple places of your website.

An untested better code would be something like this.

session_start();
$data_id = md5( time().microtime().rand(0,100) );
$_SESSION["POSTDATA_$data_id"] = $_POST;
header('Location: ' . $uri."?data_id=$data_id");

In the next page you may retrieve the previous post like this

session_start();
$post = array();
$data_key = 'POSTDATA_'.$_GET['data_id'];
if ( !empty ( $_GET['data_id'] ) && !empty( $_SESSION[$data_key] ))
{ 
    $post = $_SESSION[$data_key];
    unset ( $_SESSION[$data_key] );
}

The code above is not tested, you may have to deal with some error before it works.

like image 146
tacone Avatar answered Oct 03 '22 10:10

tacone


if u want to carry forward your POST data to another pages ( except the action page) then use

session_start();
$_SESSION['post_data'] = $_POST;
like image 32
diEcho Avatar answered Oct 03 '22 09:10

diEcho