AJAX Login - Session Problem

I have created a working login script using AJAX and PHP, but my problem is - it makes me login twice before it can remember a session. Initially it uses js response to update the contents but if I refresh the page, everything turns back to its previous state. When I login for the second time and refresh the page, it’s the only time it will work. Why is this happening?

You can check my files here:

  1. login.php -
    login.php

  2. users.php -
    users.php