0

My program is basically a form. After the user presses the submit button, the program does input error checking (so I can't use the action element in form to call a new program). After error checking I call another program using header("Location: url").

The problem is I lose all my session data. I have session_start() as the first line in both programs, I use $_SESSION['variable name'] to name some variables. My form uses the POST method.

My POST variables exist after the user presses submit but they are lost also after the header() call - along with my $_SESSION[''] variables. My session_id is the same for both programs. I have tried session_write_close() just before the header() statement - still lose the data.

How do I keep my variable data?

10
  • 1
    When you redirect, are you redirecting to the same domain? Same server? Commented Aug 8, 2014 at 16:02
  • Was about to ask the same question as FDL, are you sending to the exact same domain? www.mysite.com is different from mysite.com, cookies are not the same unless you override the domain path in the cookies which is rarely done! Commented Aug 8, 2014 at 16:03
  • 2
    “My POST variables exist after the user presses submit but they are lost also after the header() call” – of course they are, because the client browser follows the redirect by issuing a GET request for the new URL. Commented Aug 8, 2014 at 16:04
  • 1
    Code speaks louder than words. Please demonstrate the problem with the smallest amount of code necessary to reproduce it. Commented Aug 8, 2014 at 16:04
  • check session_id() in both scripts. if the value's changing, then you're getting a different session in each script, and you'll have to figure out why (probably a cookie setting problem). Commented Aug 8, 2014 at 16:18

2 Answers 2

1

Before you use your header, stock the content of your $_POST into a $_SESSION variable. The $_POST variables aren't saved if you go from page 1 --> 2 --> 3, they are sent from page 1 to page 2, that's it.

Or 2nd option : when you do the header you can put the content of your post into the url to do something like :

header("Location: http://www.example.com/index.php?name=".$_POST['name']);

And the when after the header when you land on your page you can use :

$name = $_GET['name'];

And you'll get the name for instance.

edit : ty for fixing my english :)

Sign up to request clarification or add additional context in comments.

6 Comments

My sentiments exactly. Plus, I edited that it to that's it but you re-edited back. I believe that's what you meant; least, that's how the expression goes ;)
Alex, I like your comment. However, my form is dynamically generated and can have from 50 to 250 variables. Your 2nd option looks good but the number of variables (and their names) changes with each user
Then you definately need to use the first one. You can stock all the posted variables into a session variable which will be an array, it's possible :)
@Alex: Thanks again. But please note from my original comments that I lose my $_SESSION variables data also
All your pages have the extension .php and not .html right?
|
0

When you assign those session variable .. try to echo them on the same page without the header and see it they are assigned properly.

and make sure your use exit() right after the header call. If you don't it still runs the rest of the script after redirecting you. it might be resetting the session variable since $_POST will be empty then.

let me know if it works

1 Comment

Thanks, MD. Yes, I echoed all my variables - they exist. And yes I use exit() after the header() call, I just didn't mention it in my original comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.