Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make user session go on for 24 hours?

I have messed around with my apache and php.ini file and my site's users still complain about the site logging them out after a very short time or every time they close and open the same browser.

I am running Apache and PHP.

What settings should I have in order to make the users session go for 24 hours so they don't have to re-log in every time?

Thanks, Alex

like image 917
Genadinik Avatar asked Jun 21 '11 18:06

Genadinik


People also ask

How do you make a session expire?

Session timeout can be customized, to make the user's page inactive after a fixed time. Starting session: The PHP, session_start() function is used to start a session in the web page. Syntax: session_start();

How do I turn on automatic sessions?

If there's a need to use sessions throughout your application, you can also opt in to starting a session automatically without using the session_start function. There's a configuration option in the php. ini file which allows you to start a session automatically for every request— session. auto_start .

What is the default lifetime of a session?

Session lifetime determines the maximum idle time of an end user's sign-on session to Okta. Lowering this value decreases the risk of malicious third party access to a user's applications from an active session. The maximum time allowed time for this setting is 90 days. The default session lifetime is two hours.

How long do session variables last?

By default, session variables last until the user closes the browser. So; Session variables hold information about one single user, and are available to all pages in one application. Tip: If you need a permanent storage, you may want to store the data in a database.


2 Answers

In php.ini, set:

; 24 hour session cookie
session.cookie_lifetime = 86400

; Prevent server from cleaning up session
; Some value higher than the cookie lifetime
session.gc_maxlifetime = 200000 
like image 91
Michael Berkowski Avatar answered Sep 23 '22 19:09

Michael Berkowski


Strange. Sessions should stay for quite a long time. Try checking your code for any accidental session_destroy()s.

If that doesn't work, then maybe try using cookies:

setcookie(name, value, expire); 

So, to set a cookie variable in PHP, you would simple use

<?php
    setcookie("MyCookie", "MyValue", time()+60*60*24); 
?>

The expire value is in seconds. Using the code above, you would be able to set a cookie called "MyCookie" with the value "MyValue" and lasts for 24 hours.

To retrieve the value of this cookie, you could use

<?php
    print($_COOKIE['MyValue']);
?>

Note that cookies MUST be set before the tag is called.

If cookies don't work either, then it's probably a problem with your php.ini Can you post your php.ini if cookies don't work?

Hope this helps!

like image 40
iAndr0idOs Avatar answered Sep 19 '22 19:09

iAndr0idOs