简体   繁体   中英

PHP: Server check if user is logged in or out

I'm wanting to be able to tell if a user is logged in or not from the server end. I currently have it set up where each user record in my database has an attribute 'loggedin' (boolean). If my user goes through the login process then he/she's 'loggedin' is set to '1'(true). If the user goes through the logout page I set it to '0'(false). My problem is what if they don't go through the logout page but instead their cookie times out?

In hopes of helping anyone better understand what I'm trying to accomplish. I'm just trying to make a feature that will show on user's pages how long it has been since they were last logged in. I have it currently calculating but it shows either "user is online" or "online 4 minutes ago" depending on the 'loggedin' value in the database described earlier. So, if I visit a friends page then it will tell me how long ago it's been since they've been on.

** Just thought maybe store in the database another attribute that stores the timeout time of the cookie after login, so I could check for the 'loggedin' value and their timeout value to determine which information to display. This doesn't seem to be a very good way of doing it but i don't know.

If you have a better way of doing this than the method I've described above please clue me in :)

Thanks for any information!

Use session variables:

$_SESSION['logged'] = 1;

You can use them throughout your script and their values will be kept between reloads.

create a statement like this

if (isset($_COOKIE['loggedin'])) {
    echo '<p>User is online</p>';
} elseif (!isset($_COOKIE['loggedin'])) {
    echo '<p>User Offline!</p>';
} else {
    // another statement if needed to handle errors
}

I think this could be adapted to what you need, as long as the cookie is created on the login script.

It may need a little tinkering.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM