SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php Sessions keep expiring too early

    I want to extend a session time so that a session variable does not expire until after 12 hours. Problem is that after 24 minutes (default time for a session until it expires) of inactivity it still expires the session and hence gives me undefined indexes for those SESSION variables.

    What else do I need to do in my code in order to be able to extend the sessions so that it does not expire on its own until 12 hours has passed:

    The code below doesn't work is ini_set() is above session_start();
    Code:
       <?php 
            ini_set('session.gc_maxlifetime',12*60*60); 
    ini_set('session.cookie_lifetime',12*60*60); 
            phpinfo();               
            session_start(); 
     
        ....//rest of code below 
     
            ?>
    The code below doesn't work is ini_set() is below session_start();
    Code:
        <?php 
            phpinfo();               
            session_start(); 
     
            ini_set('session.gc_maxlifetime',12*60*60); 
    ini_set('session.cookie_lifetime',12*60*60); 
     
        ....//rest of code below 
     
            ?>
    in phpinfo() it states this below for gc_maxlifetime:
    Code:
                               Local Value  Master Value 
    session.gc_maxlifetime       43200        1440 
    session.cookie_lifetime      43200          0

  2. #2
    SitePoint Zealot
    Join Date
    Jan 2011
    Location
    Portland
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you on shared hosting?
    coming soon sitejuju.com my new development portfolio

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jgetner View Post
    Are you on shared hosting?
    Well I am using a university server to place all of my php scripts. I upload php scripts from my computer into the university server. Is that classed as "shared hosting" as other students will be using the server. The admin of the server stated that it should work if I use ini_set() but it isn't working when I am using ini_set();

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2011
    Location
    Portland
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm well in most cases for myself I tend to use cookies with the session being encrypted. But after a quick search i found a work around on stackoverflow referencing a snippet off of php.net. Posted Below.

    PHP Code:

    $Lifetime 
    3600;
    $separator = (strstr(strtoupper(substr(PHP_OS03)), "WIN")) ? "\\" "/";

    $DirectoryPath dirname(__FILE__) . "{$separator}SessionData";
    //in Wamp for Windows the result for $DirectoryPath
    //would be C:\wamp\www\your_site\SessionData

    is_dir($DirectoryPath) or mkdir($DirectoryPath0777);

    if (
    ini_get("session.use_trans_sid") == true) {
        
    ini_set("url_rewriter.tags""");
        
    ini_set("session.use_trans_sid"false);

    }

    ini_set("session.gc_maxlifetime"$Lifetime);
    ini_set("session.gc_divisor""1");
    ini_set("session.gc_probability""1");
    ini_set("session.cookie_lifetime""0");
    ini_set("session.save_path"$DirectoryPath);
    session_start(); 
    coming soon sitejuju.com my new development portfolio

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So looking at the code you kindly posted for me, would you say that really I need all of the code below to be able to get this session timelife increase:

    ini_set("session.gc_maxlifetime", $Lifetime);
    ini_set("session.gc_divisor", "1");
    ini_set("session.gc_probability", "1");
    ini_set("session.cookie_lifetime", "0");
    ini_set("session.save_path", $DirectoryPath);

    By the way if my php script is lets say "helios.hud.ac.uk/u0999999/Mobile_app/Text1.php", then will $DirectoryPath be: $DirectoryPath = dirname(__FILE__) . "{$separator}SessionData"; If I want to save the session data in the SessionData.php script to make it "helios.hud.ac.uk/u0999999/Mobile_app/SessionData.php" Do I actually need "ini_set("session.save_path", $DirectoryPath);"?

  6. #6
    SitePoint Zealot
    Join Date
    Jan 2011
    Location
    Portland
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes you will need to configure all the ini directives. As for the path your just saving the session to a file so the path will need to be an absolute file path.
    coming soon sitejuju.com my new development portfolio

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, thank you very much Last question if I have multiple php scripts e.g. Text1.php, Text2.php and Text3.php, is the session file path all go into one file e.g sessionData.php or does each script need to go to its own file path

    How do mark this thread as solved. I forgot how to do it as it has been a while since I have been on sitepoint


Tags for this Thread

Bookmarks

Posting Permissions

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