Posted by: prajapatinilesh | January 14, 2009

Manually Set PHP Session Timeout — PHP Session

To find out what the default (file-based-sessions) session timeout value on the server is you can view it through a ini_get command:

// Get the current Session Timeout Value
$currentTimeoutInSecs = ini_get(’session.gc_maxlifetime’);

Change the Session Timeout Value

// Change the session timeout value to 30 minutes  // 8*60*60 = 8 hours
ini_set(’session.gc_maxlifetime’, 30*60);
//————————————————————————————–

// php.ini setting required for session timeout.

ini_set(‘session.gc_maxlifetime’,30);
ini_set(‘session.gc_probability’,1);
ini_set(‘session.gc_divisor’,1);
//————————————————————————————–
//if you want to change the  session.cookie_lifetime.
//This required in some common file because to get the session values in whole application we need to write session_start();  to each file then only will get $_SESSION global variable values.

$sessionCookieExpireTime=8*60*60;
session_set_cookie_params($sessionCookieExpireTime);
session_start();

// Reset the expiration time upon page load //session_name() is default name of session PHPSESSID

if (isset($_COOKIE[session_name()]))
setcookie(session_name(), $_COOKIE[session_name()], time() + $sessionCookieExpireTime, “/”);
//————————————————————————————–
//To get the session cookie set param values.

$CookieInfo = session_get_cookie_params();

echo “<pre>”;
echo “Session information session_get_cookie_params function :: <br />”;
print_r($CookieInfo);
echo “</pre>”;
//————————————————————————————–
Some Description of session related setting for php.ini file.

session.gc_maxlifetime integer
session.gc_maxlifetime specifies the number of seconds after which data will be seen as ‘garbage’ and cleaned up. Garbage collection occurs during session start.
session.cookie_lifetime integer
session.cookie_lifetime specifies the lifetime of the cookie in seconds which is sent to the browser. The value 0 means “until the browser is closed.” Defaults to 0. See also session_get_cookie_params() and session_set_cookie_params(). Since the cookie is returned by the browser, it is not prolonged to suffice the lifetime. It must be sent manually by setcookie().

Thanks,
Nilesh Prajapati.


Responses

  1. Just passing by.Btw, your website have great content!

    _________________________________
    Making Money $150 An Hour

  2. nice tutorial

    • ??? Where is the tutorial ???

  3. Thanks to you both guys.. i will add some more interesting things related with programming…

  4. Thanks Nileshbhai, your content is very interesting….

    it will help me in my work….

  5. Its useful.

    Thanks,
    Aryashree Pritikrishna

  6. even though you set a long session time, the php garbage collector might still kill your session cookies:
    http://www.captain.at/howto-php-sessions.php

  7. good site.. help me lot

  8. Thank for info…

  9. Hi all! This might help you better understand PHP session timeouts. Please read on. You might get the hint you need. http://markuzweb.blogspot.com/2009/11/how-to-increase-php-session-timeout.html

  10. Thanks for the tutorial..
    I thought there’s another method other than via php.ini value intervention..
    well, then I’ll stick with this method then

  11. Maja ma bhai.

    Nice posts, nice clean pleasent website. Thx for sharing.

  12. What is the reason for session not expires until logout? Is there any coding issue?

  13. Thanks for your post…. i was searching for this …. finally i found it Grater post

  14. hi…i could not set cookie in wordpress..help..

  15. Visit now http://savan9.in for change your life in smart .Thanx

  16. Nice info, lets try it

  17. Nice doc. Where can I find php.ini Sessions seem to be working in the same php page but when I move the another page they are gone.???

  18. Thanks for this post Im fighting with php session now 🙂

  19. Thanks. This helped me alot.

  20. hi bro,
    i want full script of session time out ,
    i want that my session would destroy automatically after 30 seconds, so what can i do for thnis. and i want to give this time manually on my php script

  21. thanks for this great tutorials

  22. majja ma che bhai , su kem 6e

  23. nice blog

  24. nice

  25. thanx for advice…


Leave a reply to Riley Knepel Cancel reply

Categories