RPM: How to set the session expiration time accurately in PHP

Source: Internet
Author: User
Tags phpinfo tmp folder

Originally from: http://www.jb51.net/article/52309.htm

Most of the time we use the default setting for the session expiration time, and for some special requirements we can set the session expiration time.

For this, you can set php.ini in PHP, find session.gc_maxlifetime = 1440 # (PHP5 default 24 minutes)
Here you can set the expiration time. But some people say that after the setting, it doesn't seem to work!
It doesn't really work, but because the system defaults:

?
12 session.gc_probability = 1session.gc_divisor = 1000

Garbage collection There is a probability that 1/1000 is the session 1000 times before being recycled.
As long as you have a large number of visits, it will be able to achieve the effect of recycling.
Or you can set the value of the Session.gc_divisor,
For example:session.gc_divisor = 1, so you can clearly see the effect of the session expires.

Our most common use is in the PHP program, as shown in the following example program:

?
1234 <?phpif(!isset($_SESSION[‘last_access‘])||(time()-$_SESSION[‘last_access‘])>60)$_SESSION[‘last_access‘] = time();?>

This will be done, if you want to set the expired can also be implemented in the program:

?
123 <?phpunset($_SESSION[‘last_access‘]);// 或 $_SESSION[‘last_access‘]=‘‘;?>

The session has an expiration mechanism:

Session.gc_maxlifetime Original session expiration is a small probability event, using session.gc_probability and Session.gc_divisor respectively to determine the probability of GC in the running session The default values for Session.gc_probability and session.gc_divisor are 1 and 100, respectively. The probability of a GC in the session is 1%, respectively, by numerator and denominator. If you modify these two values, you will reduce the efficiency of PHP. So this method is wrong!!
Therefore, modifying the gc_maxlifetime variable in the php.ini file can prolong the session Expiration time: (for example, we change the expiration time to 86,400 seconds)
Session.gc_maxlifetime = 86400
Then, restart your Web service (typically Apache).

When the session "Recycle" occurs:

By default, every PHP request will have a 1/100 probability of recycling, so it might simply be understood as "one recovery per 100 PHP requests". This probability is controlled by the following parameters
#概率是gc_probability/gc_divisor

?
12 session.gc_probability = 1session.gc_divisor = 100

Note 1: Assuming this situation is gc_maxlifetime=120, if the last modification time of a session file is 120 seconds ago, the session will still be valid until the next recovery (1/100 probability) occurs.

NOTE 2: If your session uses Session.save_path to save the session,session recycle mechanism, it is possible that the expired session file will not be processed automatically. You will need to periodically delete the expired session manually (or crontab):

?
1 cd/path/to/sessions; find -cmin +24 | xargsrm

The session in PHP never expires

Do not modify the program is the best way, because if you modify the program, the test department must be very depressed, then only modify the system environment configuration, is actually very simple, open the php.ini settings file, modify three lines as follows:

1, Session.use_cookies

Set this value to 1 and use a cookie to pass the SessionID

2, Session.cookie_lifetime

This represents sessionid the time that the client cookie is stored, the default is 0, which means that the browser shuts down SessionID. This is why the session of PHP cannot be used permanently! So let's set it to a number we think is big, 999999999 how, yes! That's it.

3, Session.gc_maxlifetime

This is the session data on the server side of the storage time, if more than this time, then the session data will be automatically deleted! Then we also set it to 99999999.

That's all OK, of course, if you don't believe it, just test it--set a session value after a 10-day half-month return to see if your computer is not powered down or down, you can still see this sessionid.

Of course, you may not have control of the server permissions and not as fortunate as I can modify the php.ini settings, all depend on ourselves there is a way, of course, we must use the client to store cookies, the resulting sessionid stored in the client's cookie, Set the value of this cookie and pass this value to the function session_id (), as follows:

?
123456789 <?phpsession_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的值 ?>

Session invalidation does not pass

Let's write a PHP file: <?=phpinfo (), and upload it to the server to see the configuration of the server's parameters.
Go to the session section and see that the SESSION.USE_TRANS_SID parameter is set to zero.
This parameter specifies whether transparent SID support is enabled, that is, whether the session is passed along with the URL. My personal understanding is that once this parameter is set to 0, then each URL will start a session. In this way, the page cannot be traced back to the previous page of the session, which is what we say cannot be delivered. Two pages generated two session files on the server side, and no association. (Here the precise principle remains to be confirmed)
So one way is to change the value of Session.use_trans_sid to 1 in the config file php.ini.

Of course, we know that not everyone has the authority to change the configuration of PHP, then what is the indirect solution?
Here are two examples to illustrate:
File 1 test1.php

?
12345678910 <?php//表明是使用用户ID为标识的sessionsession_id(SID);//启动sessionsession_start();//将session的name赋值为Havi$_SESSION[‘name‘]="Havi";//输出session,并设置超链接到第二页test2.phpecho "<a href="test2.php" rel="external nofollow" >".$_SESSION[‘name‘]."</a>";?>

File 2:test2.php

?
12345678 <?php session_id (SID); //Start session session_start (); //The session passed in the output test1.php. echo "This is" . $_session [ ' name ' ]; ?>

So, the focus is on session_start (), preceded by session_id (SID), so when the page is converted, the server uses the session in the server session folder, which resolves the issue of delivery.
But some friends will reflect that, in this way, multiple users of the session is written in a SID, the value of the session will not play out. So there is a workaround for this problem, without adding session_id (SID), if you have configured permissions on the server's php.ini:
Output_buffering change to ON, the truth is not the table.
The second possible reason is that you do not have read permissions to the folder where the session was saved, or return to phpinfo.php to see the address saved by the session:

?
1 session.save_path: var/tmp

So just check to see if the Var/tmp folder is writable.
Write a file: test3.php to test:

?
123 <?phpechovar_dump(is_writeable(ini_get("session.save_path")));?>

If you return bool (false) to prove that the file folder permission is limited, then change folders to add in the page you wrote:

?
1234567 //set the current directory session subfolder to save the path for the session. $sessSavePath = dirname ( __file__ '/session/' //If the new path is readable and writable (can be implemented by changing the folder property to 777 on FTP), let the path take effect. if ( is_ Writeable ( $sessSavePath ) && is_readable ( $sessSavePath { session_save_path ( $ Sesssavepath }

Go: How to set the session expiration time accurately in PHP

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.