Home  >  Article  >  Backend Development  >  How to make the session in php never expire?

How to make the session in php never expire?

青灯夜游
青灯夜游Original
2020-07-21 10:59:154120browse

Open the php.ini file and set the "session.use_cookies" value to 1, the "session.cookie_lifetime" value to 999999999, and the "session.gc_maxlifetime" value to 99999999. That's it.

How to make the session in php never expire?

How to never expire session in php

The first method Method:

Open the php.ini settings file and modify the three lines as follows: [Related tutorial recommendations: "PHP Tutorial"]

1. session.use_cookies

Set this value to 1, use cookies to pass sessionid

2, session.cookie_lifetime

This represents the time that SessionID is stored in the client cookie, the default is 0 , which means that the SessionID will be invalidated as soon as the browser closes it... It is because of this that the PHP session cannot be used permanently! So let's set it to a number we think is big, how about 999999999, that's ok! that's all.

3. session.gc_maxlifetime

This is the time that Session data is stored on the server side. If this time is exceeded, the Session data will be automatically deleted! So let's also set it to 99999999.

That's it, everything is ok. Of course, if you don't believe it, just test it and see - set up a session and come back after 10 days and a half to see if your computer is not powered off or down. , you can still see the sessionid.

Second method:

Of course, it is also possible that you do not have permission to control the server and cannot modify the php.ini settings. Of course, you must use the client to store cookies. , store the obtained sessionID in the client's cookie, set the value of this cookie, and then pass this value to the session_id() function. The specific method is as follows:

<?php 
session_start(); // 启动Session 
$_SESSION[&#39;count&#39;]; // 注册Session变量Count 
isset($PHPSESSID)?session_id($PHPSESSID):$PHPSESSID = session_id(); 
// 如果设置了$PHPSESSID,就将SessionID赋值为$PHPSESSID,否则生成SessionID 
$_SESSION[&#39;count&#39;]++; // 变量count加1 
setcookie(&#39;PHPSESSID&#39;, $PHPSESSID, time()+3156000); // 储存SessionID到Cookie中 
echo $count; // 显示Session变量count的值 
?>

Note: 'PHPSESSID in the setcookie line 'Not necessarily. If you meet a network administrator who suffers from modification mania, he may have modified it. The best way is to use the phpinfo() function to check and confirm the session.name item. The value is relatively scientific.

Related topic recommendations: php session (including pictures, texts, videos, cases)

The above is the detailed content of How to make the session in php never expire?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn