How do I check if a user is enactive for a period of time please?

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
IChaps
php-forum Fan User
php-forum Fan User
Posts: 131
Joined: Tue Mar 01, 2016 3:07 pm

Sun Oct 13, 2019 2:54 pm

Hello.

Could I please just enquire.
I'm about to write/test some code that will check the current time and the last time a user used their account.
If a user has been enactive for 15 minutes, then switch their status to offline, or log them out.

At present I'm playing with the following code:-

Code: Select all

if(session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}
include($_SERVER['DOCUMENT_ROOT'].'/system/config.php');
$servername = $_SESSION['dbf_host'];
$username = $_SESSION['dbf_user'];
$password = $_SESSION['dbf_pass'];
$dbname = $_SESSION['dbf_databasename'];


try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $results = $conn->query('SELECT * FROM client');
    while ($row = $results->fetch()) {
        $now = time();
        if ($row['online']>0) {
            $chk_date = strtotime($row['reg_date']);
            $datediff = $now - $chk_date;
            $day_elapsed =($datediff / (60 * 60 * 24));
            if ($day_elapsed > 14) {
                //Mark user offline
                $userid = $row['client_id'];
                $sql = "UPDATE client SET online='0' WHERE client_id='$userid'";
                //Prepare statement
                $stmt = $conn->prepare($sql);
                // execute the query
                $stmt->execute();
            }
        }
    }
}catch(PDOException $e) {}
But I can't get it to work , and I have a cron schedule setup to run the above code, which also don't work.

I'd just like to request a better or correct way of achieving what I'm trying to do?
I'm also not sure how to get the database details, ie name, username and password. If I use a session it's not recognised, and if I access the details direct from the config file, I get path not found. This only happens with the code above, and not with the rest of my project.

Thank You
IChaps
php-forum Fan User
php-forum Fan User
Posts: 131
Joined: Tue Mar 01, 2016 3:07 pm

Mon Oct 14, 2019 6:26 am

Hi.

I think I may have sorted this query out.

Code: Select all

<?php
//Add new member
if(session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

$servername = $_SESSION['dbf_host'];
$username = $_SESSION['dbf_user'];
$password = $_SESSION['dbf_pass'];
$dbname = $_SESSION['dbf_databasename'];

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $results = $conn->query('SELECT * FROM client');
    while ($row = $results->fetch()) {
        $now = time();
        $your_date = strtotime($row['reg_date']);
        $datediff = $now - $your_date;
        $day_elapsed =($datediff / (60 * 60 * 24));
        if ($row['online']>1) {
            if ($datediff > 10) {
                $userid = $row['client_id'];
                $sql = "UPDATE client SET online=0 WHERE client_id='$userid'";
                //Prepare statement
                $stmt = $conn->prepare($sql);
                // execute the query
                $stmt->execute();
            }
        }
    }
}catch(PDOException $e) {}
?>
However, I still don't yet, if it will work off a cron job.

Thanks.
Post Reply