I want display logged in online users on my site like Members Online: 102
. I am not catching a perfect and simple way as how to do this. Whether there be a session counting method or any other method to achieve this result. I can do it by using a 0 and 1 column in database table while logging in and logging out but it will not help when the user directly closes the browser and exits the session. That's the real problem. Please help me how to do this. For any instance I am just sharing my login and logout script.
Logout Script
<?php
session_start();
if(isset($_SESSION["zoranelogin"])){
unset($_SESSION["zoranelogin"]);
header("Location: login.php");
}
?>
Login Script
<?php
session_start();
if(isset($_SESSION['zoranelogin'])){
header('Location: dashboard.php');
}
include'config/db.php';
$msg = null;
$date = date('Y-m-d H:i:s');
$uname = (!empty($_POST['uname']))?$_POST['uname']:null;
$pass = (!empty($_POST['pass']))?$_POST['pass']:null;
$mpass = (!empty($_POST['pass']))?md5($_POST['pass']):null;
if(isset($_POST['login'])){
$chklogin = "SELECT * FROM members WHERE mem_uname = :uname AND mem_pass = :pass";
$chklogiq = $pdo->prepare($chklogin);
$chklogiq->bindValue(':uname', $uname);
$chklogiq->bindValue(':pass', $mpass);
$chklogiq->execute();
$checklgn = $chklogiq->rowCount();
$fetch = $chklogiq->fetch();
if($checklgn > 0){
session_start();
$_SESSION['zoranelogin'] = $fetch['mem_id'];
header("Location: dashboard.php");
}else{
$msg = "<div class='message-error'>Username and Password are incorrect. Please try again!</div>";
}
}
?>