Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Header Location [duplicate]

Tags:

php

header

Possible Duplicate:
PHP header() redirect with POST variables

I'm writing a script where form data is being submitted to another script. I want the second script to perform some error-checking on the submitted $_POST data, and if everything is okay, process the data. If the data has errors, I'm using header('Location: http://www.example.com/script.php'); to return the visitor back to the form page.

The issue I'm having is that I want the form to be sticky - fields with correct data maintain the values the user put in them. Obviously, to get those values, I need access to the $_POST array. However, this seems to be destroyed when the header() call forwards the visitor back to the form.

Is there any way to use the header Location stuff to redirect the visitor to another page, while still preserving the $_POST data?

Now I'm using like this : header('Location: http://www.example.com/add.php?id='.$id.'&name='.$name.'&code='.$code.'&desc='.$description.''); and accessing as $_GET.

For the same can I use and kind of POST in header('location: xxx')??

like image 865
Vijay Avatar asked Aug 31 '12 10:08

Vijay


1 Answers

the best way to achieve this 'sticky form' is to use a session.

<?php
session_start();
$_SESSION = $_POST;
//do error checking here
//if all is valid
session_write_close();
header('Location: *where you want your form to go*');
die;
?>

and on the redirect page you can use them like so:

<?php
session_start();
//use $_SESSION like you would the post data
?>
like image 171
Mic1780 Avatar answered Sep 21 '22 09:09

Mic1780