Keeping PHP Sessions open, or any sessions open regardless of what language you use, can be a pain. One of the most commonly asked questions is “How do I keep my sessions open for 30 days or more?”.
This is written from the perspective of PHP, but of course Cookies themselves can be manipulated from whatever server-side language you use to code your web application.
So if your users only stay logged in for a few hours, maybe a a couple of days at the most, and you want them to stay logged in much longer, continue reading.
The main reason this method works is because it doesn’t rely on PHP to keep the session, and sessions on the server tend to get cleared out, which is probably the most common reason for users having to login again. Rather than rely on PHP garbage collection to work properly, you take control of a secondary session that you store yourself. When the primary login check fails (depends on your application), you check the secondary session against your own records, and if you find a match, you can auto-login the user, returning control to the PHP session itself. You can choose to be seamless about the whole process, or you can present your user with one of those unnecessary messages saying “Please wait while we find your account…”. (more…)