Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,096
    Thanks
    15
    Thanked 1 Time in 1 Post

    Question set duration/timeout of a PHP session

    Is there a way that I can set the duration/timeout of a PHP session at my site at http://www.ayrshireminis.com ? Currently I am setting a PHP session, for the language of the site, by using the following code:

    PHP Code:
        session_start();
        if(!isset(
    $_SESSION["lang"])) {        
            
    $_SESSION["lang"] = 'de';
        } 
    I know there is a way to set the timeout in ASP, an the same be done in PHP as I have went back to the page after a while and the session is lost?

    Picco

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    I think you need to use a cookie or set the lifetime of sessions with a .htaccess file if you don't have access to the php configuration though this will effect all sessions.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Regular Coder
    Join Date
    Oct 2002
    Posts
    178
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by crmpicco View Post
    Is there a way that I can set the duration/timeout of a PHP session at my site at http://www.ayrshireminis.com ? Currently I am setting a PHP session, for the language of the site, by using the following code:

    PHP Code:
        session_start();
        if(!isset(
    $_SESSION["lang"])) {        
            
    $_SESSION["lang"] = 'de';
        } 
    I know there is a way to set the timeout in ASP, an the same be done in PHP as I have went back to the page after a while and the session is lost?

    Picco
    Immediately after initially starting the session, you could store the current time. Then each time the script loads, you could compare the current time against that setting. If the time difference is greater than what you want, then kill the session.

    That way it's all done on the server side.

    Example (not tested since I don't have access to my server right now, but it should at least give you the idea):

    PHP Code:
    session_start();
    if(
    is_set($_SESSION['startTime'])){
      
    $timeDiff time() - $_SESSION['startTime'];
      echo 
    "You've been logged in too long.";

      
    //destroy session
      
    $_SESSION = array();
      
    session_destroy():
      
    setcookie('PHPSESSID'''time()-3600'/'''0,0);
    }else{
      
    $_SESSION['startTime'] = time();

    Last edited by doni; 03-26-2008 at 03:04 AM. Reason: Added example code

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,044
    Thanks
    2
    Thanked 316 Times in 308 Posts
    He actually wants the session to persist.

    The problem with this is a session is meant to last for the duration of a single visit to a site. The session cookie lifetime and the garbage collection maxlifetime both limit the how long a session will persist.

    If you want to do this, you must read the session handling section in the php manual. In addition to the other settings that control how long a session will persist, if you are on a shared host, you will need to set the session cookie path to a private folder so that the session_starts() in all the other scripts won't delete your session data files.

    To save a user preference like a language setting for longer than a single visit to your site, you either need to use a cookie just for that setting or if you have a login/authentication cookie, store the language setting in a database and retrieve it using the login/authentication information.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    Regular Coder
    Join Date
    Oct 2002
    Posts
    178
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Ahh--I didn't realize we were looking to persist the session array over multiple browser sessions. I assumed we were talking about coming back to the SAME browser session a few hours later (the browser hadn't been closed).


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •