Home Backend Development PHP Tutorial 关于用户在线与离线的判断有关问题

关于用户在线与离线的判断有关问题

Jun 13, 2016 am 11:19 AM
data nbsp php session

关于用户在线与离线的判断问题!
首先需求是做一个显示在线用户昵称和在线人数。

然后个人的思路是在用户表里面做一个字段用来标记用户在线(1)或者不在线(0),当用户登录的时候就设置这个字段为1,当用户通过正常渠道(即点击安全退出)退出的时候就设置这个字段为0.这样就显示在线用户的话就可以定时刷新一次,这种情况下功能是没有任何问题的,但是例外情况就是用户不点击安全退出而是直接就关闭浏览器了,那这样这个用户岂不是永远都在线了?所以我的问题是如何处理当用户通过非正常渠道离开之后也能检测到他是否在线。

请大神帮忙~~~~


------解决方案--------------------
每个页面中都加入公共js文件,里面的某个js函数负责定时向某php文件(如a.php)发送ajax请求。最后请求时间由a.php每次写入session

比如设定每2分钟发送一次请求。在需要获取用户是否在线的页面上,读出session里的最后请求时间,当前时间减去最后请求时间,若大于2分钟,则用户已离线。
------解决方案--------------------
这个问题网上可以顺手找到很多案例。
如果想采用最轻巧的,可以统计session临时文件数量或其它存储方式的单元数量,来确定当前20分钟内有多少在线。当然,这个时间精确度可以缩小到一分钟(随之而增长的是会话回收机制的消耗)。
无论采取任何方案,这个功能还是对服务器有不少消耗,如果不是很必要,建议放弃。
------解决方案--------------------
正确的思路并且不借助memcache等第三方工具的方法是
分析:在线统计难点是在于不存在全局共享的方法
解析办法:
利用文件来保存在线用户的信息,这样对文本的处理实际上还比数据库快很多(php连数据库的时间就已经超过了php读取一个小文本的时间)
文本里保存的格式为数组,格式为

1

<br /><?php<br />return array(<br />   20312=>13671927362,//key值为用户uid    value值当用户登陆的时间戳<br />   20313=>13671927363,<br />);<br />

Copy after login

1:当用户登陆时,获取当前时间戳,session记录用户id
2:然后获取文本里的文件,里面保存了在线用户的信息,判断当前uid是否存在这里面,不存在就插入,然后继续保存
3:在线人数获取,就读取文件,然后count出在线人数
4:只要当访问这个文件的时候(不管你是准备统计在线人数,还是有新用户加入),遍历文本里的数据,判断各个用户的时间戳是否小于当前时间戳并超过半小时,如果是,那么就去掉这个用户信息.
实现步骤关键点:
以数组获取文本
$online=require 'xxx.php';
以数组保存文本
$data[1111]=123748492;
file_put_contents('xx.php','');
不谢!
------解决方案--------------------
存入普通文件的关键,是写入时候的资源独占问题。
------解决方案--------------------
引用:
每次session临时文件是靠sessionId,sessionId不一样,生成临时文件目录都不一样.
我存在的疑问:
1:能够读取临时文件夹的session文件吗?它是否有被加密过?还是纯文本?
2:如果是文本?里面存的就是session的内容?那么那些数组,对象是如何被存入文本的?(数组还可以,对象是怎么存入文本的?)

1、当然可以读取 session 临时文件。你可以在 session.save_path 指定的地方找到他们
2、session 临时文件是纯文本文件。是用简化的序列化格式存储的,php 已然提供了 session 序列化和反序列化的函数。你自己看一下就知道了

你们都在讨论如何存储的问题,其实如何存储并不是主要问题
关键在于“如何识别”,既然一致认为“非正常渠道离开”的检查是需要轮询的。那就把的种方案都弄出来PK一下就是了(假定有一万个用户在线)
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

See all articles