Home  >  Article  >  Backend Development  >  How to implement php session without expiration

How to implement php session without expiration

藏色散人
藏色散人Original
2020-07-28 09:42:552408browse

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.

How to implement php session without expiration

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[&#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: 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!

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