How do I keep the session after browser closing?

7

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 resolve this?

    
asked by anonymous 26.01.2016 / 04:09

2 answers

5

In PHP the session time directive in php.ini is sesion.cookie_lifetime .

A session uses a file, in the php temporary folder whose name is a hash.

Each session in turn uses a cookie. By default it is called PHPSESSID . In this session the file name of the previously mentioned session is saved.

From there, the data allocated in $_SESSION is saved, serialized (yes, by the serialize function itself), in that session file.

Thus, PHP reads the value of the PHPSESSID cookie and checks to see if that file is in the session folder. If it is, it reads this data and sends it to the $_SESSION variable.

And why am I explaining this?

Some people think that the session has no connection to the cookie, but it does. So much that if you delete browser cookies, as it will not have the information to be captured by php, the session user will be logged off, for example.

And if php uses cookies in parts, then you can change the way that cookie is configured.

As I've explained, the directive responsible for the lifetime of the session is 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 be only until the browser closes.

This can be perceived through the session_get_cookie_params function, which shows how the current php session cookie configuration 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 session_set_cookie_params function to resolve this. To use it you must set your settings before the session_start function.

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 required. So we can use this function by passing only the argument that will represent the lifetime of the session.

So, let's do this:

$time = 2 * 60 * 60; // Defini 2 horas

session_set_cookie_params($time);
session_start();

I hope this helps: D

Links

Update

The author of the question asked me to define the session "infinitely". The solution to this is to set the maximum value for int allowed by php as the argument of session_set_cookie_params . Let's use the constant PHP_INT_MAX .

Example:

session_set_cookie_params(PHP_INT_MAX);
    
26.01.2016 / 12:11
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.

In order for you to keep the data even after closing the browser, use Cookies that is similar to the session / session, but the difference is that the log is stored on the client's computer, so every time a visitor is requested site it checks if it has some cookies corresponding to that site and if it has it already it takes the data saved and it does the validation in the correct form.

To create a Cookies the syntax is simple:

setcookie("nome_do_cookie", "valor_do_cookie");
But since it is not feasible to explain here how to use cookies from the beginning I will leave here the php reference link about cookies for you to take a look and the link of a very interesting tutorial, Good luck.

Php Cookies Reference : link

Cookies Use Tutorial : link

I hope I have helped.

    
26.01.2016 / 04:36