orange
orange

Reputation: 341

how do php share data without access a DB

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

Upvotes: 1

Views: 119

Answers (3)

Dolph
Dolph

Reputation: 50720

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.

Upvotes: 1

Pascal MARTIN
Pascal MARTIN

Reputation: 401182

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.

Upvotes: 0

Ganesh Shankar
Ganesh Shankar

Reputation: 4864

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...

Upvotes: 0

Related Questions