简体   繁体   中英

PHP: Sessions to check if user is logged on

H guys, I am currently working on a mobile application which has a login and register page. I have setup the login and register page, and it works great.

The table that holds the user credentials have the following fields:
username, fname, lname, password, logged_status

when the user "logs on" it should set the status of that user to logged on. How can i achieve this?

**please note, that my android application connects to a web server, which runs different PHP files, depending on the function being executed (for example, if the user wants to login, the login.php file is executed on the server*

除了使用PHP端使用android端并使用SharedPreference登录存储外,还有许多可用示例,这样您的用户只要进入应用程序就可以显示为已登录状态,除非他从应用程序注销。

make the logged_status status enumerated dataType with 0 and 1 flags, 0 being default.

when a user successfully login update the logged_status

<?php

$stmt = $dbh->prepare("SELECT * FROM users WHERE email = ?");
$stmt->execute([$_POST['email']]);
$row = $stmt->fetch();

if ($row && password_verify($_POST['password'], $row['pass']))
{   
    //update logged_status status
    $update = $dbh->prepare("UPDATE users SET logged_status ='1' where email= ?");
    if($update->execute([$_POST['email']])){
        // set sessions and redirect

    }
} else {
    echo "invalid";
}
?>

As Aniket have suggested its better if you do this using android than php.

you can use boolean values to represent the logged on status so that on a user log in in you can set the value to 1 for logged in and on logging out you set it to 0 for not logged in.

But it would be much better to write a function on the android side that keeps calling a php script on the server to update the status so that if a user is inactive for a long period, their status is turned to not logged in in your table.

Let me know what you think

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