Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6855841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:48:33+00:00 2026-05-27T01:48:33+00:00

I’m building a custom PHP Session handler using MySql, and I’ve been struggling with

  • 0

I’m building a custom PHP Session handler using MySql, and I’ve been struggling with something. After a throughout (or not so much, subject to personal opinion) search in Stack Overflow, I couldn’t find an answer to my question: most just answer how to expire a session in the client-side, but this question is focused on the server-side.

The issue is the following: I store the session data on a SQL database, and send an id of the session as a cookie (basic session handling); if the user selected not to be remembered (aka keep me logged on/remember me) the cookie should expire on the event of the browser closing, but for obvious reasons it wouldn’t expire on the database (this is not intended, but it’s pretty hard to tell when a browser closes from the server side); moreover, if the user selected to be remembered, the cookie shouldn’t expire, and the user should be signed in automatically when he starts the browser again.

What I would like to do is expire the session on the database when the browser is closed (if the user doesn’t select remember me), and to keep the session alive in both ends if the user selects remember me, but assign the user a new session if he logs in again (and unset/delete the previous one).

All for the sake of security, I absolutely want to void the sessions that won’t be used again; for example, when the user closes the browser, the cookie is deleted and the session won’t be accessed ever again legitimately by the corresponding user, so it should be deleted.

My current idea is to use double sessions: having two cookies with two different ID’s and cookies, in which one of the cookies would expire when the browser is closed and one would remain, and the latter would be used to reference the primary one, delete it, and generate a new one and logging in the user back to the new one (if the user selected to be remembered). The double sessions don’t necessarily need two databases, as they could just be stored on the same table with different fields.

My main focus is to keep the database clean and the system safe. How would you manage session expiration on the server side?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:48:33+00:00Added an answer on May 27, 2026 at 1:48 am

    First, there is no way to be certain of when the user has closed the browser. Using two cookies doesn’t help, as the user has control of the browser and can chose to persist the cookie you think will disappear when the browser closes and delete the persistent tracking cookie you are checking for to delete the original session. This means your solution could potentially never expire the session on the server.

    A better solution is to write an expiration date/time (say 30 minutes from now) to the database session record. Every time the user tries to access the session check to see if the expiration date/time has passed. If the expiration has passed, delete the session and deny the user access. If the expiration has not passed let the user in and update the expiration date/time (30 minutes from now, just like when you created the session). This means if the user hasn’t accessed your application in 30 minutes you are assuming the user has closed the browser and are expiring their session. As long as the user does something at least once every 30 minutes the session will remain active on the server side.

    To clean up sessions that have expired and the user hasn’t come back (or maybe cleared their cookies or something) you can have a background process that runs maybe once a day to delete expired sessions.

    You can use the same strategy for long-term remember-me type sessions by just using an expiration date/time that is much longer (maybe 14 days from the current date/time).

    Personally I store sessions in a Redis hash (Redis is a NoSQL in-memory database) which allows me to set an expiration for the hash and Redis automatically deletes it when the hash expires, saving me from having a background process to delete old sessions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.