0

I want to be able to keep a PHP session, even after the browser has been closed. I know on this question, they suggested using the session_set_cookie_parameters() parameter, but I am unable to find a working example of this that doesn't expire and works across all directory on my domain. If someone could help out, it would be amazing.

Community
  • 1
  • 1
Harry
  • 3,301
  • 7
  • 27
  • 33
  • I wanted to avoid using cookies because they are client side, and therefore can be edited or modified by the client; henceforth making it easier for people to gain unauthorised access to an others account. – Harry Jan 14 '13 at 03:54
  • so store the session id in the cookie and recreate when they come back –  Jan 14 '13 at 04:04
  • That's what I ended up doing. Thanks. :) – Harry Jan 14 '13 at 06:44

2 Answers2

1

Use session_set_cookie_parameters() with a non zero value before session starts.

Refer here.

Vishnu R
  • 1,859
  • 3
  • 26
  • 45
1

Session stands for "until browser is closed". Session is something that expires. If you don't want it to be expired, you're probably don't want a session at all. You better read on cookie too. I guess that's what you need.

But You can use session_set_cookie_parameters() to give the session cookie a non-zero lifetime before starting the session, or set session.cookie_lifetime to non-zero.

Remeber session_set_cookie_params() needs to be called before session_start() for every single page request. Read more

Code:

<?php 

session_set_cookie_params(30 * 60, "/");
session_start();
print_r(session_get_cookie_params());

?>

Output:

Array ( [lifetime] => 1800 [path] => / [domain] => [secure] => )

Explanation:

Sets the cookie to expire after 30 minutes and be available anywhere on the site.

Techie
  • 44,706
  • 42
  • 157
  • 243