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();

   <?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();

    <?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:

                           Local Value  Master Value
session.gc_maxlifetime       43200        1440
session.cookie_lifetime      43200          0 

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();

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.



$Lifetime = 3600;
$separator = (strstr(strtoupper(substr(PHP_OS, 0, 3)), "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($DirectoryPath, 0777);

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();



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);”?

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.

Ok, thank you very much :slight_smile: 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