Home > Article > Backend Development > How to implement php session without expiration
php session non-expiration method: first use the client to store the cookie; then store the obtained sessionID in the client's cookie; then set the cookie value; and finally pass the value to the "session_id()" function That’s it.
Share the solution ideas and implementation methods of session never expiring in PHP
Open php. ini settings file, modify the three lines as follows:
Related recommendations: "php session session (topic)"
1, session.use_cookies
Set this value to 1 and 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 represents the browser. The SessionID is invalidated as soon as it is closed... It is because of this that the PHP session cannot be used permanently! So let's set it to a number that we think is very large, how about 999999999, that's okay! That's it.
3. session.gc_maxlifetime
This is the time that the Session data is stored on the server side. If it exceeds this time, the Session data will be automatically deleted! Then we 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.
Of course, it is also possible that you do not have the authority to control the server and are not as lucky as me to modify the php.ini settings. We have ways to rely on ourselves. Of course, we must use the client to store cookies, right? The obtained sessionID is stored in the client's cookie, sets the value of this cookie, and then passes this value to the session_id() function. The specific method is as follows:
<?php session_start(); // 启动Session $_SESSION['count']; // 注册Session变量Count isset($PHPSESSID)?session_id($PHPSESSID):$PHPSESSID = session_id(); // 如果设置了$PHPSESSID,就将SessionID赋值为$PHPSESSID,否则生成SessionID $_SESSION['count']++; // 变量count加1 setcookie('PHPSESSID', $PHPSESSID, time()+3156000); // 储存SessionID到Cookie中 echo $count; // 显示Session变量count的值 ?>
Note: In the setcookie line The 'PHPSESSID' in is not certain and may have been modified. The best way is to use the phpinfo() function to check and confirm the value of session.name, which is more scientific.
The above is the detailed content of How to implement php session without expiration. For more information, please follow other related articles on the PHP Chinese website!