4

Just a quick question.

I have a form that captures a bit of data, validates it and then posts to another page on the same site. I'm using header() to redirect if there are no errors, but obviously the data is not sent in the post.

Would I be correct to use a session to persist the data between the two pages?

Thanks guys.

hakre
  • 193,403
  • 52
  • 435
  • 836
mrbubbles
  • 687
  • 6
  • 19
  • 2
    Yep. This is totally acceptable. – Rawkode Apr 05 '13 at 12:00
  • the only problem is if you get in a situation where you need more than 1 machine serving your app. Then you have problems with Sessions... and have to move to persisting the data in a shared data source like a cache or a DB – Brad Bonkoski Apr 05 '13 at 12:00
  • 1
    I think dealing with load balancers and multiple servers is a bit of a jump from the initial question, while still technically valid - I guess. – Rawkode Apr 05 '13 at 12:01
  • Uh sorry for leaving some reference. Naturally he must do nothing, can and should do what he wants. As far as a technical thing is concerned I prefer some reference instead of opinions. Not that it is always possible to give, but hypertext to the rescue. – hakre Apr 05 '13 at 12:13
  • 1
    @Rawkode. Agreed. Your answer gave the desired input. – mrbubbles Apr 05 '13 at 12:17

5 Answers5

3

If you only need the data on the second page and if the data is not sensitive, you might as well pass the data in the header().

$url = "www.example.com?variable=value&id=something";
I.e. header('Location: $url');
Jay Bhatt
  • 5,601
  • 5
  • 40
  • 62
0

These kinds of "flash messages" are fine and are a good way to prevent the user from being able to reload the page and resubmit the form if you are worried about that. I would remind you to unset the session vars after you display the message.

jcbwlkr
  • 7,789
  • 2
  • 22
  • 28
0

Yes, it is correct approach.

However, depending on situation, there are other alternatives also such as hidden variables, iframe, etc. If you could explain the entire situation, depending on the nature of data, I can provide you much precise answer.

Murtuza Kabul
  • 6,438
  • 6
  • 27
  • 34
0

You can store the data on the server side (Session) or on the Client side (Cookie). What way you select depends on the kind of data and security issues.

Also you can insert posted data via PHP directly in the new form without saving it - but that will not work it if you redirecting via header

Ilya
  • 169
  • 6
0

Yes you can do that, but to be even more safe, add that in db with extra field of DUMMY or something. by this if you need to discard you can discard the dummy record. But you need to have time stamp so you dont delete those records which are recently added and are in process.

you can also create cron job to empty the table

Aamir Mahmood
  • 2,704
  • 3
  • 27
  • 47