Question:
Check That User Is Logged In With PHP?
?
2011-06-09 14:22:55 UTC
Good day. I'm am currently developing a website using PHP (no framework), i am able to log the user in and out successfully but i would like to know how i can use PHP to check if another user is logged in while the current user is logged in. Please, how may i go about this?
Three answers:
anonymous
2011-06-09 17:20:41 UTC
It's difficult to check this for other users, because you don't want to continually update every user their pages. You can do the following:



First make another column in your table with users, name it lastlogintime for example.

When a user logs in you update lastlogintime with the current time (a timestamp is sufficient).

When a user goes to another page (while logged in) you update lastlogintime again, with the current time.

When a user logs out you just update the lastlogintime to 0.



When you do it like this you can just compare the lastlogintime of each user to the current time. When the difference is more then (for example) 5 minutes you see him as logged off (he's actually logged of, and has a lastlogintime 0, or wasn't active in the last 5 minutes). When the difference is smaller (he was active in the last 5 minutes), you will see the user as logged in.



Creating a timestamp: time().

Time() creates a timestamp in seconds, so a difference of 5 minutes would be 300 seconds.
anonymous
2016-12-11 00:04:13 UTC
Use a cookie in javascript! once the person has logged in, set a cookie on his device. If he logs-off, delete the cookie. On loading the domicile web page, examine if the cookie exists. If it exists, log the person immediately, and redirect to inspite of web page you want. ($_SESSION gained't paintings, because the consultation disappear once the person leaves the positioning.)
Lee Butler
2011-06-09 14:29:09 UTC
On the login page, on the event of the login form being submitted, copy this:







then, when you want to check if they are logged in, run an IF command, and check if "$_SESSION['isloggedin'] = '1'", and if so, then they are logged in.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...