1

I am making a login system. I made active_session table in database like this:

| id | user_id |session_key|others...|
| 12 |  6548   |kdjgs939493|.........|

I create a cookies with limited life time (like 1 hour or day etc). When I logout manually it destroys cookies and remove session record from database as well. The problem is that if someone login and doesn't logout, the cookies will expire after it time expires but how can i remove such sessions from database. because if these records are not deleted then there will be lot of useless sessions records in database

2
  • session_start(); session_unset(); session_destroy(); Commented Feb 18, 2016 at 12:31
  • I know these, but these are not going to delete records from database automatically Commented Feb 18, 2016 at 12:33

2 Answers 2

2

You will have to store a timestamp, either when the session was started or when it should end. Then you can run scheduled tasks and delete all entries which you don't need any more.

Sign up to request clarification or add additional context in comments.

Comments

0

Solution 1: You can add a command to delete old entries in the same script that performs the login. This way, you assure that after each login, there will be no old records.

Solution 2: You can make a script that executes regularly, to check for older records in the database and delete them. This way, you could use a specific mysql user for this script with right to delete records.

1 Comment

both are right, i added expirydate column in the sessions table whose default value is current time + (time of session expiry or whatever), and included this query in login script: $deloldsession = $con->query("DELETE FROM active_sessions where expirytime < NOW()");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.