8
I log in to my site, but when I close the browser, there is no more session. Then I have to log in again.
How do I fix it ?
8
I log in to my site, but when I close the browser, there is no more session. Then I have to log in again.
How do I fix it ?
6
In PHP the directive responsible for session time on php.ini
is the sesion.cookie_lifetime
.
A session uses a file, in the php temporary folder, whose name is a hash.
In turn every session uses a cookie. By default it is called PHPSESSID
. In this session the file name of the aforementioned session is saved.
From there, the data allocated in $_SESSION
are saved, serialized (yes, by function serialize
same), in this session archive.
Thus, PHP reads the value of the PHPSESSID cookie and checks whether that file is in the session folder. If it is, read this data and send it to the variable $_SESSION
.
And why am I explaining this?
Some people think that session has no connection to the cookie, but it does. So much so that if you delete browser cookies, as you will not have the information to be captured by php, the session user will be logged out, for example.
And, if php uses in parts cookies in the session, then you can change the way this cookie is set.
As I explained, the directive responsible for the lifetime of the session is the session.cookie_lifetime
.
By default its value is set to 0
(zero). And we all know that if we set a cookie with the value 0
, its duration will only be until browser close.
This can be perceived through function session_get_cookie_params
, which shows how the current configuration of the php session cookie is.
Come on:
print_r(session_get_cookie_params());
Notice the result:
Array
(
[lifetime] => 0
[path] => /
[domain] =>
[secure] =>
[httponly] =>
)
And how to change that?
Use the function session_set_cookie_params
to solve this. To use it you should set your settings before the function session_start
.
See the "function skeleton":
void session_set_cookie_params (int $lifetime [,string $path [,string $domain [,bool $secure = false [,bool $httponly = false]]]] )
Only the parameter $lifetime
is mandatory. Then we can use this function by passing only the argument that will represent the session life time.
So let’s do it this way:
$time = 2 * 60 * 60; // Defini 2 horas
session_set_cookie_params($time);
session_start();
I hope this helps :D
Another alternative to setup (with ini_set, if you have access to your hosting): how to Keep Session after browser close
The author of the question asked me about defining the session "infinitely". The solution to this is to set the maximum value for int allowed by php as argument for session_set_cookie_params
. Let’s use the constant PHP_INT_MAX
.
Example:
session_set_cookie_params(PHP_INT_MAX);
It worked :D, a doubt.. and for me to leave Session permanent until the user clicks Unset or clear the browser data ?
@Permanent williamalvares would be through a gambiarra. Defines how 100 years, for example, it will work. Example: 60 * 60 * 24 * 365 * 100
You need to adjust and understand how Session.gc_maxlifetime works too, otherwise the Session file can be deleted before the cookie.
And in this case, if the session is 100 years old, you can put a nice hard drive in the machine to save all the open sessions...
0
Friend the session/Session works as follows: It stores the data in the browser or it will leave the data stored while the browser is not closed, from the moment the client closes the browser the session data is lost, This is exactly the feature of the session.
So that you can keep the data even after closing the browser use Cookies that is similar to the session/Section but the difference is that the log is stored on the client’s computer, then every time a visit to such site is requested it checks if it has any cookies corresponding to that site and if it has it already takes the saved data and makes the validation in the correct way.
to create a Cookie the syntax is simple:
setcookie("nome_do_cookie", "valor_do_cookie");
however how it is not feasible to explain here all the way to use cookies from the beginning I will leave here the reference link of php about cookies for you to take a look and the link of a very interesting tutorial, Good luck.
Reference php Cookies: http://php.net/manual/en/features.cookies.php
Tutorial on the use of Cookies: https://www.codigofonte.net/dicas/php/149_trabalhando-com-cookies-no-php
I hope I’ve helped.
Wrong statement: In php the session is not "saved in the browser", but only the name of the session file is saved in the browser. If it were saved in the browser, the user could change the session value (and that would be very bad). So I make sure to make that clear here. And the problem with using the cookie is that, depending on the size of the information you want to store, it won’t do the job.
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
There is no way to resolve this friend, this is the feature of the session, to keep this use cookies
– Leonardo
It is "solve", but you should understand that Session in php is a session cookie and the correct behavior is to delete the cookie when closing the browser. Maybe tomorrow I’ll post an answer.
– Guilherme Nascimento
I notice that people like to make generic statements like "there’s no way to do this" or "there’s no such thing", but this can be solved. Frameworks like Laravel configures whether the session will be destroyed when closing the browser or for a period (2 hours for example). Actually, you can set a Lifetime for the php session.
– Wallace Maxters
William, I’ve taken the liberty of editing the title. I think this way the description of your problem becomes more detailed.
– Wallace Maxters
The most suitable solution is to keep the PHP session as normal, and to make the persistence of what is most important in DB + Cookie separate (and to reload when the session "wins"). In an ideal world, simply increase the PHP session with
cookie_lifetime
next to thegc_maxlifetime
(one is not enough), but in the "real world" it is not so simple.– Bacco