doujiang1913 2014-02-02 06:42
浏览 15

用户活跃和在线

I am going to be building a private chat function for my site. I basically have the code but need to changed it about and add a table for it in the database. I will be modifying a PM system I already have in place basically.

I have included my script that checks to see my visitor is logged in. This is done by a current session and if they have closed their browser without logging out when they come back to the site they will automatically be logged in.

This is the check user code

<?php
session_start();
include_once("db_conx.php");
// Files that include this file at the very top would NOT require 
// connection to database or session_start().
// Initialize some vars
$user_ok = false;
$log_id = "";
$log_username = "";
$log_password = "";
// User Verify function
function evalLoggedUser($conx,$id,$u,$p){
    $sql = "SELECT ip FROM users WHERE id='$id' AND username='$u' AND password='$p' AND activated='1' LIMIT 1";
    $query = mysqli_query($conx, $sql);
    $numrows = mysqli_num_rows($query);
    if($numrows > 0){
        return true;
    }
}
if(isset($_SESSION["userid"]) && isset($_SESSION["username"]) && isset($_SESSION["password"])) {
    $log_id = preg_replace('#[^0-9]#', '', $_SESSION['userid']);
    $log_username = preg_replace('#[^a-z 0-9]#i', '', $_SESSION['username']);
    $log_password = preg_replace('#[^a-z0-9]#i', '', $_SESSION['password']);
    // Verify the user
    $user_ok = evalLoggedUser($db_conx,$log_id,$log_username,$log_password);
} else if(isset($_COOKIE["id"]) && isset($_COOKIE["user"]) && isset($_COOKIE["pass"])){
    $_SESSION['userid'] = preg_replace('#[^0-9]#', '', $_COOKIE['id']);
    $_SESSION['username'] = preg_replace('#[^a-z 0-9]#i', '', $_COOKIE['user']);
    $_SESSION['password'] = preg_replace('#[^a-z0-9]#i', '', $_COOKIE['pass']);
    $log_id = $_SESSION['userid'];
    $log_username = $_SESSION['username'];
    $log_password = $_SESSION['password'];
    // Verify the user
    $user_ok = evalLoggedUser($db_conx,$log_id,$log_username,$log_password);
    if($user_ok == true){
        // Update their lastlogin datetime field
        $sql = "UPDATE users SET lastlogin=NOW() WHERE id='$log_id' LIMIT 1";
        $query = mysqli_query($db_conx, $sql);
    }
}
?>

how the chat will work is that I will set a ENUM with a value of 0 or 1 to a database row within the users table what will be called online. On log in this will go to 1 and on log out this will go to 0. When the row is set to 1 they will be displayed to their friends in the chat menu (bottom right) and when they are not online they will not show in the bottom right.

That will work well as long as the users log out after every visit to the website and I am pretty sure that 99% will not do that what means they will always be showed as online in the private chat.

I know there is a way to check if someone is currently online as facebook does it and many other sites do. I was just wondering/hoping that someone on here would be able to point me into the direction on how to make the script to check to see if someone is currently online and to update this ENUM row automatically (if that is the method that should be used) when they have left the site and update it once again when they have come back to the website.

The reason I have included my check login status script is because I am not sure if the code will need to be in here as this script is included in every page on the website.

I do believe that javascript maybe needed for this and that is why I tagged it.

Thank you for reading and I look forward to your help.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程