0

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>";
    }
}
?>

1 Answers1

0

It's not possible to detect a closing browser. The best way to know how many users you have on the site is to do it all with sessions. When you use the sessions on file system it's pretty hard because you have to open all the files and read them. So....

The best way to do it is to save your sessions in the database. There you can do set a user id to an session on login and remove it on logout. Then you can count all sessions with an user is with a simple mysql count script.

Here a link with a nice tutorial how to do sessions on a database.

http://www.tonymarston.net/php-mysql/session-handler.html

You still have one more problem. When a user closes his browser you will not see that. This is because a client don't send a message. i'm gone now. That's why we have a session timeout. you can shorten the timeout to say 2min but then you have a problem that when someone is reading a page for more than 2min he will be kicked. You can compensate that with an ajax script every min to update the session but that will take a lot of server traffic/memory. That's why that is not recommended.

Advice: Set your sessions to the database with an timeout of 20min. When someone closes his browser you won't see that for max 20min.

Paules
  • 540
  • 1
  • 3
  • 13
  • what if user is active for more 20 mins continuously? will he still be kicked on completion on 20 mins timer? –  Feb 04 '17 at 15:28
  • This did not solved my query but still I got an idea to do this for now. An unofficial method, maybe with not so cool logic but can be done.. :) –  Feb 04 '17 at 19:55
  • Not after 20min. On session_start() the session handler will execute SessionHandler::gc($maxlivetime). This function will clean all inactive sessions on the system. Check http://php.net/manual/en/sessionhandler.gc.php for more info. – Paules Feb 05 '17 at 09:43