1

I would like to set my sessions to last for 2-3 days so that my users will not have to login to get data after days of being idle.

Please help me with the code to achieve this.

Treffynnon
  • 21,365
  • 6
  • 65
  • 98
A12
  • 21
  • 1
  • 2
  • 1
    If you want a session to last that long, it would mean not closing the browser and/or remaining inactive for 2-3 days. Look into cookies. – Trent Apr 12 '11 at 10:34
  • It's not a good idea to use session for this functionality, also changing session.gc_maxlifetime will decrease your application portability. try to use Cookies instead – Omid Apr 12 '11 at 10:45
  • **possible duplicate of [How do I expire a PHP session after 30 minutes?](http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes)** – Gordon Apr 12 '11 at 10:46
  • 1
    Please use the search function before asking duplicates. Thanks. – Gordon Apr 12 '11 at 10:47

4 Answers4

2

You could try:

ini_set('session.gc_maxlifetime', 2*60*60*24); // 2*60*60*24 = 2 days
Twelve47
  • 3,924
  • 3
  • 22
  • 29
2
<?php
$value = 'something from somewhere';

setcookie("TestCookie", $value);
setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */
setcookie("TestCookie", $value, time()+(3600*24*2));  /* expire in 2 days */
?>

http://php.net/manual/en/function.setcookie.php

gmadd
  • 1,146
  • 9
  • 18
1

Do not increase session time for a huge amount of time.

Instead you can use the remember me cookies for your purpose.

You can create a cookie to store user name and password set the time for cookies life time to 2 or 3 days.

Whenever user enter your site check for existence of these cookie if they exists log in them backgroundly and redirect them on landing page.

Whenever the logout delete these cookies as well.

Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
0

You can do by,

ini_set('session.gc_maxlifetime', 2*60*60*24); //2 days

But as session will destroy as soon as your user close the browser alternate you can use cookies.

setcookie("TestCookie", $value, time() + 2*60*60*24);
Rikesh
  • 26,156
  • 14
  • 79
  • 87