Mr.Tanav
Mr.Tanav

Reputation: 107

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*

Upvotes: 0

Views: 787

Answers (3)

Masivuye Cokile
Masivuye Cokile

Reputation: 4772

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.

Upvotes: 1

Douglas Hosea
Douglas Hosea

Reputation: 1022

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

Upvotes: 0

Aniket Jadhav
Aniket Jadhav

Reputation: 166

Instead of using PHP side use android side and store logged in using SharedPreference there are many examples available, that way your user will show as logged in whenever he enters the app unless he logs out from it.

Upvotes: 1

Related Questions