PHP session never expire to solve ideas and methods

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags cookie course customer customer service developed it is log in network

Let PHP session never expire, you may not have encountered such a depressing problem, but I encountered, very depressed.

We developed a system that only corporate customer service staff can use - a limited number of customer service staff. This is a limited number of customer service staff a few days ago suddenly raised such a problem: We every very short period of time (half an hour does not operate the page), are anxious to solve customer problems, the system has prompted the need to log in, delayed Customer's time ... this is unhappy!

Customer is God, the only God. So on the peak request we can achieve PHP session never expire, unless our customer service man let him expire. For security reasons I do not understand this never-ending behavior; for reasons of laziness I really do not want to change my previous program. But there is no way, I still need to change.

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

1, session.use_cookies

Set the value of this to 1 and use the cookie to pass the sessionid

2, session.cookie_lifetime

This on behalf of the SessionID cookie stored in the client, the default is 0, on behalf of the browser closed SessionID void ... Because of this PHP session can not be used permanently! Then we set it to a number we think it is great 999999999 how kind, yes! That's it.

3, session.gc_maxlifetime

This is Session data stored in the server-side time, if more than this time, then the Session data is automatically deleted! Then we also set it to 99999999.

This is all OK, of course, you do not believe it then test to see - set a session value after a period of 10 days and a half back to see if your computer is no power or downtime, you can still see this sessionid.

Of course, you may not have the authority to control the server and not as lucky as I can modify the php.ini settings, all rely on ourselves is a way, of course, must be used to store the client cookie, the bar sessionID stored to the customer End of the cookie inside, set the value of this cookie, and then pass this value to the session_id () function, as follows:

<? php session_start (); // Start Session $ _SESSION [count]; // Register Session variable Count isset ($ PHPSESSID)? session_id ($ PHPSESSID): $ PHPSESSID = session_id (); // If $ PHPSESSID is set, It will be SessionID assigned to $ PHPSESSID, or generate SessionID $ _SESSION [count] ++; / / variable count plus 1 setcookie (PHPSESSID, $ PHPSESSID, time () + 3156000) // show the value of the Session variable count?> If long time later (how long you look at it) you come back to refresh this page, the output of the figures than you go when a big 1 it! If a lot larger, it is estimated Who moved your computer, the test is not accurate, huh, huh ... ... go out for a while now!

Note: 'PHPSESSID' in setcookie's line is not a must. If you encounter a network administrator with amending madness, he may have modified it. The best way to do this is to use the function phpinfo () Look, confirm the value of a session.name, more scientific.

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.