1

I know that sessions are server side, so, it is possible to save a session even if the browser is closed?

For example save a session for one day.

Please do not suggest "cookies", in this case must be implemented sessions.

thanks

user773961
  • 141
  • 3
  • 11
  • 5
    Sessions usually use cookies, so your question is automatically about cookies – Pekka May 31 '11 at 15:04
  • ok, i know that, but you understand what i am talking :) – user773961 May 31 '11 at 15:05
  • serialize the session and save it in a file – Khurram Ijaz May 31 '11 at 15:05
  • 3
    Some people are way confused about sessions. The session hash reference is stored in a Cookie. The session data is store Server side, please read up on sessions before you go spurting out mal-information. – Jim May 31 '11 at 15:07
  • Modern browsers support local storage, as the name suggests it's on the client and can be read/written via javascript – konsolenfreddy May 31 '11 at 15:07
  • 3
    Usually a cookie is used to remember the session ID. How would you make the browser/user remember the session ID without a cookie? Pass the session ID in the URL and let him/her bookmark it? – Gumbo May 31 '11 at 15:08
  • @G molvi: PHP session files ARE serialized copies of $_SESSION. – Marc B May 31 '11 at 15:23

6 Answers6

1

They are saved already (see your php.ini file for the session path)... In fact, the real issue lies in garbage collecting them.

If you want to store them longer, edit your php.ini file or, define a custom session handler:

http://www.php.net/manual/en/function.session-set-save-handler.php

Denis de Bernardy
  • 75,850
  • 13
  • 131
  • 154
1

session_set_cookie_params I think is what you are looking for. If you are storing the session in a cookie, this will allow you to set the lifetime of that cookie. So the user can come back anytime within that time frame and still have their original session.

Side Note

Give this a read for more about session lifetimes etc. How do I expire a PHP session after 30 minutes?

Community
  • 1
  • 1
Jim
  • 18,673
  • 5
  • 49
  • 65
1
php_value session.gc_maxlifetime 86400

You can set the above in .htaccess or modify session.gc_maxlifetime in php.ini

This defines how long PHP will have a session file for the user on the server before garbage collection (the example above will allow the server to maintain the sessions for 1 day), however sessions generally do rely on a session id cookie so if the browser is reset or clears the cookie the user won't re-attach to their web session (you are actually setting a session ID cookie to use sessions in most cases even if you don't realise it.)

neopickaze
  • 981
  • 8
  • 14
1

You can create a database and store there the sessions and on client side just store $_SESSION['id'] wich is the id of the session in the database. But this will become a headache when you will have to store more and more variables in the session.

Octavian
  • 4,519
  • 8
  • 28
  • 39
1

Like Gumbo said, pass it in the URL. But how I like to solve this is, instead of passing the SESSION_ID through the Url, just make it a hash, or encoded data.

Then whenever this user comes to your page. you can check in your headers if this hash/encoded-data is still in the valid time frame, and if this 'anonymous' user has permissions for thay zone.

THE DOWNSIDE: If this user passes around this link, anyone could access the data

THE UPSIDE: Extremely portable, and easy to implement


Store the SESSION_ID in a database bound to the users IP. whenever this user logs back in, start the session via setting the SESSION_ID with session_id

THE DOWNSIDE: A lot more work, and if the users ISP changes their generated IP regularly this won't work

THE UPSIDE: Even if he erases the SESSION_ID cookie you will be able to continue the session

Community
  • 1
  • 1
Dudemullet
  • 400
  • 1
  • 7
  • Also, I'm taking as a given that you are setting the sessions max lifetime (as other users have suggested). Due to this just being 'good use' of the tech, as well as valid domains and such – Dudemullet May 31 '11 at 15:50
0

there are many ways to do this but beign an artisan you could:

make an script that save each session for your users inside a file

OR

go to PHP.ini and change the session life time

OR

use the session_set_save_handler function more info here

Frank Leal
  • 212
  • 4
  • 18