webForumDet fria alternativet

Sessionslängd fungerar inte

PHPur PHP

1 svar · 151 visningar · startad av lillebror

Medlem sedan apr. 20041 597 inlägg
Frågan#1

Hej,

Jag har ett problem som jag måste försöka lösa så snart det bara går. I min portal har jag satt sessionslängden till 60 minuter med följande kod:

ini_set("session.gc_maxlifetime", "3600");

Nu verkar inte detta fungera. En användare berättade att informationen i formuläret gått förlorat då meddelandet skulle skickas och webbläsaren uppdaterades. Detta är inte bra om det är lång artikel som har författats. Då försvinner allt! Det verkar som att sessionslängden inte är en timme som jag har satt den till.

Vad är det som är fel?

Medlem sedan apr. 20041 597 inlägg
#2

Hittade följande kod på nätet:

<?
// path for cookies
$cookie_path = "/";

// timeout value for the cookie
$cookie_timeout = 60 * 30; // in seconds

// timeout value for the garbage collector
//   we add 300 seconds, just in case the user's computer clock
//   was synchronized meanwhile; 600 secs (10 minutes) should be
//   enough - just to ensure there is session data until the
//   cookie expires
$garbage_timeout = $cookie_timeout + 600; // in seconds

// set the PHP session id (PHPSESSID) cookie to a custom value
session_set_cookie_params($cookie_timeout, $cookie_path);

// set the garbage collector - who will clean the session files -
//   to our custom timeout
ini_set('session.gc_maxlifetime', $garbage_timeout);

// we need a distinct directory for the session files,
//   otherwise another garbage collector with a lower gc_maxlifetime
//   will clean our files aswell - but in an own directory, we only
//   clean sessions with our "own" garbage collector (which has a
//   custom timeout/maxlifetime set each time one of our scripts is
//   executed)
strstr(strtoupper(substr($_SERVER["OS"], 0, 3)), "WIN") ? 
	$sep = "\\" : $sep = "/";
$sessdir = ini_get('session.save_path').$sep."my_sessions";
if (!is_dir($sessdir)) { mkdir($sessdir, 0777); }
ini_set('session.save_path', $sessdir);

// now we're ready to start the session
session_start();

session_register('mytest');
print "mytest=".$_SESSION['mytest']."<br><br>";
$_SESSION['mytest'] = "captain";
?>
123 ms totalt · 3 externa anrop · v20260731065814-full.25f56b17
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
121 ms — hämta tråd, inlägg och bilagor (db)