0

What are some streamline ways to pass objects from one PHP page to another? Would creating a $_SESSION best fit this situation?

Thanks in advance, Smccullough

UPDATE: I'm messing around with the Facebook PHP SDK & trying to better my practical PHP knowledge. The object I want to pass would contain Facebook album IDs and the photo ID for the album cover. The size of this object could be as little as two album IDs and two photo IDs, or bigger than 1000 album and cover photo IDs. Completely depends on the user.

Smccullough
  • 363
  • 1
  • 7
  • 21
  • Have a look at http://stackoverflow.com/questions/132194/php-storing-objects-inside-the-session – 472084 Oct 25 '11 at 16:20
  • If you use the session to store your object, you'll need to serialize/unserialize when saving and retrieving. – sissonb Oct 25 '11 at 16:22

5 Answers5

2

$_SESSION is probably what you want. Alternate methods would be browser cookies, query string parameters, and POST data.

PHP $_SESSION examples

It is also possible to use PHP sessions when users have not enabled cookies: Session ID passing

pix0r
  • 31,139
  • 18
  • 86
  • 102
1

What you should do is let the object sleep and save it to same place(etc: session), then wake it up in another page. sleep and wakeup.

xdazz
  • 158,678
  • 38
  • 247
  • 274
1

Yes, but be aware of session time outs and how much data is going to be hanging around. Also be aware that people may have cookies disabled etc etc.

Joe
  • 46,419
  • 33
  • 155
  • 245
1

You've got a few options:

1. pass by url (get queries)
2. pass by post (hidden form fields)
3. pass by cookie
4. pass by session

Without knowing any details of how big these objects of yours are, sessions are most likely the easiest, as they're purely server-side. The only thing transmitted to the client is the session ID in a cookie.

Anything else is roundtripped through the client and subject to being hacked/edited/stolen.

Marc B
  • 356,200
  • 43
  • 426
  • 500
1

Really it depends what data it is and what you are going to be doing with that data.

you can, as previously mentioned use one of these methods:

  1. pass via form
  2. pass via cookie(s)
  3. pass via session variable(s)
  4. pass via database (i.e. saving to database then reading from it on next page - still requires some kind of session or cookie to store an id for the table in the database though)

I personally like to either use sessions or cookies, or if its information I want to be able to track/debug at a later date then saving it to the database is often a good idea.

If you could provide more insight into what objects you are trying to store I'd be able to help further.

Nick
  • 6,316
  • 2
  • 29
  • 47