views:

40

answers:

3

I have two pages and I want to pass data to each other.

How can I do this without accessing a DB?

Sessions? Cookies? someother magical way?

If you know how, can you please post sample code?

Thanks

+1  A: 

Session variables is one way:

$_SESSION["variable"] = "value";

This variable can then be read/modified by another page.

Also note, that you need to start the session by calling start_session(); at the beginning of your script.

Dolph
A: 

And Cookies are another way... You can also try writing in and out of a file instead of a DB

How does a user get between these two pages? I assume a Form based solution is out of the question...

Ganesh Shankar
A: 

Amongst the possibilities, here are some that I think about :

  • You could $_SESSION (see Session Handling) -- if both pages are accessed by the same user, without too much time between the two accesses, so the session doesn't expire.
  • You could store your data to a file ; that'll work fine if :
    • The amount of data is big
    • You want it to persist for a long time
    • But you'll have to do some cleaning-up by yourself
  • Another idea would be some external daemon, like memcached
    • But, as it's a caching engine, it's not necessarily good for storing data : the data that is cache can be removed from the cache even if it has not expired yet (i.e. if there is no place left in cache, memcached will remove some least used data)
  • Of course, if the data is small and you don't mind it going back and forth through the network, and both pages are accessed by the same user using the same browser, you could use cookies


Only a couple of possibilities, though ; my preferences would probably be :

  • $_SESSION
  • or files

Depending on your situation.

Pascal MARTIN
I like the idea of sessions. Do you have any sample code to show how I can register a session and start using them?
orange