just a small query...
how to get User's login time and logout time...???
as well as how to know whether particular user in Logged in out..??
Thnx
The reliable way is to record three fields in your users table: last_login, last_seen, and last_logout (store UTC). is right about using timestamps; the trickier part is handling the user who closes the browser. You cannot detect that event server-side with certainty, so follow @Josh Connerty’s approach of updating a heartbeat on each request and treat a user as offline after an idle window (e.g., 5 minutes). Sessions alone answer, "is this one request authenticated?" but they do not let other users see presence unless you also write to a shared store (DB or cache).
On login, set both last_login and last_seen. On every authenticated page or AJAX request, refresh last_seen. On explicit logout, set last_logout and destroy the session. Throttle writes to avoid excessive updates, and index by id so the update is O(1). Example pattern:
// login
$now = time();
$db->prepare("UPDATE users SET last_login=?, last_seen=? WHERE id=?")
->execute([$now, $now, $userId]);
// per request heartbeat (throttle to 60s)
if (isset($_SESSION['uid'])) {
if (empty($_SESSION['last_seen_write']) || time() - $_SESSION['last_seen_write'] > 60) {
$db->prepare("UPDATE users SET last_seen=? WHERE id=?")
->execute([time(), $_SESSION['uid']]);
$_SESSION['last_seen_write'] = time();
}
}
// logout
$db->prepare("UPDATE users SET last_logout=? WHERE id=?")
->execute([time(), $_SESSION['uid']]);
session_destroy();
// who is online (5-minute window)
SELECT id FROM users WHERE last_seen >= UNIX_TIMESTAMP() - 300; To ’s performance concern: one lightweight indexed UPDATE per user per minute is typically fine. If you need tighter accuracy, reduce the throttle; if you need fewer writes, increase it. Optionally add a small JS ping on visibilitychange to update last_seen when the tab is closed or backgrounded, but still rely on the idle timeout as the source of truth.
Jump to Post— darkagn 315You can get the current timestamp from the time function and parse it to a date through the use of the
Jump to Post— OmniX 21nish123, you need to do research before posting here all the time!!!
For a timestamp like suggested by "darkagn" you would use the date() function supplied by php.
Go to www.php.net to …
Jump to Post— Josh Connerty 20Basicly the logic is like this, :P
You take the time stamp when they log in, store this in two fields last log in time and last movement.Every page they visit you need to runa query to update their last move to the current time stamp.
Most …
You can get the current timestamp from the time function and parse it to a date through the use of the date function. If you do this when the user logs in or out and store it in your database, you can then retrieve this information quite easily. Have a try and post an attempt, then we might be able to give you some more pointers.
okay...
.
.
but how to get logout time..??
.
.
what to do when user closes the broswer instead of logout...
.
.
i uses insert query on login to get login time with help of now function of mysql...!!
.
.
but i dont understand what to do on logout..!! though i uses update query on logout... but i m confused what to do when user closes the browser..?? :(
nish123, you need to do research before posting here all the time!!!
For a timestamp like suggested by "darkagn" you would use the date() function supplied by php.
Go to www.php.net to learn more.
You then would follow this logic
- You would store that value
-- Storing it in a database or a session (which you should have researched in your previous thread)
- Then retrieve it as required
Let me know how you go once you do some research.
Basicly the logic is like this, :P
You take the time stamp when they log in, store this in two fields last log in time and last movement.
Every page they visit you need to runa query to update their last move to the current time stamp.
Most systems use 5 minutes so I will to.
$timestampFromTable = "sometimestamp";
$timenow = time();
$lastMove = $timenow - $timestampFromTable;
$fiveMins = 60*5;
if( $lastMove > $fiveMins ) {
echo "User not logged in...";
} else {
echo "User is logged in";
} when time is echoed what format is used?
I usually use date as I can specify the format I wish to use?
when time is echoed what format is used?
I usually use date as I can specify the format I wish to use?
But you store it as a timestamp right?
when time is echoed what format is used?
I usually use date as I can specify the format I wish to use?
time() returns the unix timestamp, you have to use strftime to convert it to a human readable date
just a small query...
how to get User's login time and logout time...???
as well as how to know whether particular user in Logged in out..??
Thnx
Answer:
When we press the Login button on that time you can note down the time using timestamp method and store it into the table for reference and same with logout. means just check when we press the button , save the time in a veriable and store it into the database for later use
Basicly the logic is like this, :P
You take the time stamp when they log in, store this in two fields last log in time and last movement.Every page they visit you need to runa query to update their last move to the current time stamp.
(...)
I quote this reply to put forward some questions:
1) an approach like that (with an update query) is not slowing down the website? It's not better to save last move in sessions?
2) is there a way to save a timestamp when the user closes the browser w/o logging out or uses the same tab to visit external sites showing he's in fact left our website?
You could but this would involve JS and would be avoidable then you would have 100's of people logged in when their not.
As long as you have a decent host it shouldn't effect the server. And as for saving the last move in the session it is difficult to take data from the sessions, something I have never done.
I have always used this method and it hasn't made the site laggy at all and full fills it's purpose. :)
I love how you guys have just taken over the poor man's thread... haha
I think his disappeared anyways... lol
Best way imo, sessions and then if required outside of user login/logout database, but I doubt there would be very many scenarios that would 'require' it maybe just a little feature but session the way to go, agreed?
I love how you guys have just taken over the poor man's thread... haha
I think his disappeared anyways... lolBest way imo, sessions and then if required outside of user login/logout database, but I doubt there would be very many scenarios that would 'require' it maybe just a little feature but session the way to go, agreed?
Nope, disagreed. :)
I just siad no. I have actually looked into it and as far as a know you can't retreive cookies from users to decide wether they are on or not.
You would have to run a query to tell the sql db that the user (that just changed a page) is online. Then give it a timestamp and then run a query when displaying online users saying:
// 360 = 5mins
if( $row['timestamp'] > ( time() - 360 ) ) {
echo "Online";
} else {
echo "Offline";
} We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.