Don't use the Database to track users login State. Have a look at $_SESSION
-Array.
Sessions will timeout according to your timeout value set in php.ini
.
Helpful links:
a session is unique per user - so you wont have any conflicts, even when handling 50 Million users at the same time.
Imagine a $_SESSION
to be an array that is UNIQUE to a certain user. (actually it's nothing more, the server willt take care on providing the correct array, if the user send the correct session-cookie - which in turn is nothing you (or the user) need to take care of - his browser will do!)
In a nutshell: Free your mind of handling multiple sessions by comparing ids. The array $_SESSION
will ALWAYS be the one associated with the current user, if started properly.
Your server will always return exactly one Session-Array
, when accessing $_SESSION
- the one, associated with the user, performing the request.
Create a simple "script", like this:
<?php
SESSION_START();
if (!isset($_SESSION["start_time"])){
echo "Session started at: ";
$_SESSION["start_time"] = date("Y-m-d H:i:s");
}else{
echo "Session already existing. Started at: ";
}
echo $_SESSION["start_time"];
?>
Access the page from 2 different Browsers.
- On first access, you will receive the
Session started at
message.
- Second acces will deliver the
session already existing
Message with the already stored DateTime, UNTIL you call session_destroy()
- or the session times out.
- You can refresh the page 10.000 times - the timestamp wont change anymore as long as the session is valid AND you are using the same browser (which transmits the session cookie automagicaly :P).
- You don't need to provide any
user_id
to make this work. Your server will (silently) provide you the correct $_SESSION
-Array. Just write your website as if it would target one user, while storing user-sensitive values in $_SESSION
-Array rather than $someOther
-Array.