1

Let's say the User named Michael is registered, and is in our database in table named users.

Michael browses my site, and while he does so, I delete his user from the table. My login system works with sessions.

After user Michael was deleted from the database, the browser can browse the website with "Michael" until the session ends.

That's because after the session processes, it doesn't change unless you check every time.

My solution that may increase a huge engine load because of too many queries / requests:

In session.inc.php add a checker:

if (isset($_SESSION['user']))
{
    $check = $pdo->prepare("SELECT * FROM users WHERE user_name = :user")
    $check->execute(array( ":user" => $_SESSION['user']));

    if (!$check->rowCount())
    {
        session_destroy();
    }
}

It will check if theres a row with the exact same username he is logged in currently with the session, if not, it will destroy the session.

Question:

Will that solution cause a large engine load, if my website gets many browsing users?

Is there a better method of doing this?

2
  • user name should be unique hence no need for the limit clause. Commented Apr 21, 2013 at 13:15
  • 3
    For this check, SELECT COUNT(*) FROM users WHERE user_name = :user should be enough. Faster than fetching all data. Commented Apr 21, 2013 at 13:20

2 Answers 2

3

When you delete a row from "users" table just delete or expire the session of that user.

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

1 Comment

So when I delete a user from an admin panel, I can simply do Destory_session($username); ?
0

Ayush - How are you going to destroy someone else's session?

user1761494 - The best method is to save sessions in database, this not only adds security to your website, also gives you full control over sessions. when you kick a user out (delete/ban) just delete the session record for that user along with it.

1 Comment

earlier i had assumed that there was a field in database for session and whenever he deletes session firstly, he sets that filed inactive then deletes it....

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.