Home Backend Development PHP Tutorial Write a program for user online display_PHP tutorial

Write a program for user online display_PHP tutorial

Jul 21, 2016 pm 04:06 PM
author Write exist online start article hour show user of program were able

At the beginning of this article, the author assumes that the reader can already write a user login authentication program.
-------------------------- --------------------------------
The counter can complete the total number of times the web page is visited, but it cannot After knowing the dynamic record of visits in a period, let's introduce how to write a method to dynamically display the visits in each period.

To record the visits, we must first create a database in mysql. Let's give This database is named line, and a data table named line is created. The fields in the table are "user name (name varchar (20)), time (time datetime)". Of course, readers can also add data tables as needed. field.

After establishing the database, you can start designing the program. Now let’s clarify the ideas. If you want to display the number of visits, of course the database must have records. I have assumed that the reader has the ability to write a The user has logged in to the program, so adding records to the database can be done in the login.php file:

Pay the current time first: $time=date('Y-m-d H:i:s');
mysql_select_db(line);
mysql_query("insert into line (name,time) values('$name','$time')");

Okay, now everyone Logged-in users have a record in the database. Let’s complete the program line.php for online user display:

mysql_connect("local","","");
mysql_select_db(line);
$result=mysql_query("select * from line");
$num=mysql_numrows($result);
if (!empty($num)) {
echo "

";
for($i=0;$i<$num;$i++){
$name=mysql_result($result,$i,"name");
echo "";
}
}
?>

The above program can display the number of all online users and their respective User name, of course, this program is still very imperfect. If one of the users logs out, the database should not have a record of this user. Therefore, a delete function must be added to the logout program, assuming it is logout.php:

mysql_select_db(line);
mysql_query("delete from line where name='$name'");

At this time, a basic user online function has been completed, and then continue on line Add code to .php to make the function more complete. First, we have to specify how long the user will be considered to have left when he does not continue to browse line.php. Here, a time limit of 5 minutes is given, which means that the program will display the The user situation is 5 minutes before now, so a current time must be set in line.php to tell the program to start executing from this time, and then when the program is executed, the time recorded in the database minus the current time is deleted and all records greater than 5 minutes are deleted. , so that any user can see all online users within 5 minutes when executing line.php. To complete this function, the following database statement is required:

delete from line where timeminute)

But there is another problem is that if a user has been executing line.php for more than 5 minutes, the program must identify the user and continue To display the user, you have to use cookies to update the time record of the database. Because it is login authentication, there will be a cookie to remember the user's information. Assume that the cookie variable that records the user's name is $cookiename ( The specific variables depend on the cookie settings), and the rest is easy to handle. Use this cookie variable to complete the database modification:

update line set time='$time' where name='$cookiename '

Let’s improve line.php:

//Set the current time
$time=date('Y-m-d H:i:s');
mysql_connect("local","","");
mysql_select_db(line);

//Update user's record
mysql_query("update line set time='$time ' where name='$cookiename'");

//Delete user records older than 5 minutes
mysql_query("delete from line where time
$result=mysql_query("select * from line");
$num=mysql_numrows($result);
if (!empty($num)) {
echo "
";
echo "The number of people online now is: $num";
echo "
";
for ($i=0;$i<$num;$i++){
$name=mysql_result($result,$i,"name");
echo "";
}
}
?>

Good user online display function completed.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315471.htmlTechArticleWhen starting this article, the author assumes that the reader can already write a user login authentication program. --- -------------------------------------------------- ----- The counter can complete access to the web page...
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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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)

How to use Xiaohongshu account to find users? Can I find my mobile phone number? How to use Xiaohongshu account to find users? Can I find my mobile phone number? Mar 22, 2024 am 08:40 AM

With the rapid development of social media, Xiaohongshu has become one of the most popular social platforms. Users can create a Xiaohongshu account to show their personal identity and communicate and interact with other users. If you need to find a user’s Xiaohongshu number, you can follow these simple steps. 1. How to use Xiaohongshu account to find users? 1. Open the Xiaohongshu APP, click the &quot;Discover&quot; button in the lower right corner, and then select the &quot;Notes&quot; option. 2. In the note list, find the note posted by the user you want to find. Click to enter the note details page. 3. On the note details page, click the &quot;Follow&quot; button below the user's avatar to enter the user's personal homepage. 4. In the upper right corner of the user's personal homepage, click the three-dot button and select &quot;Personal Information&quot;

How to make Google Maps the default map in iPhone How to make Google Maps the default map in iPhone Apr 17, 2024 pm 07:34 PM

The default map on the iPhone is Maps, Apple's proprietary geolocation provider. Although the map is getting better, it doesn't work well outside the United States. It has nothing to offer compared to Google Maps. In this article, we discuss the feasible steps to use Google Maps to become the default map on your iPhone. How to Make Google Maps the Default Map in iPhone Setting Google Maps as the default map app on your phone is easier than you think. Follow the steps below – Prerequisite steps – You must have Gmail installed on your phone. Step 1 – Open the AppStore. Step 2 – Search for “Gmail”. Step 3 – Click next to Gmail app

Log in to Ubuntu as superuser Log in to Ubuntu as superuser Mar 20, 2024 am 10:55 AM

In Ubuntu systems, the root user is usually disabled. To activate the root user, you can use the passwd command to set a password and then use the su- command to log in as root. The root user is a user with unrestricted system administrative rights. He has permissions to access and modify files, user management, software installation and removal, and system configuration changes. There are obvious differences between the root user and ordinary users. The root user has the highest authority and broader control rights in the system. The root user can execute important system commands and edit system files, which ordinary users cannot do. In this guide, I'll explore the Ubuntu root user, how to log in as root, and how it differs from a normal user. Notice

How can I make money by publishing articles on Toutiao today? How to earn more income by publishing articles on Toutiao today! How can I make money by publishing articles on Toutiao today? How to earn more income by publishing articles on Toutiao today! Mar 15, 2024 pm 04:13 PM

1. How can you make money by publishing articles on Toutiao today? How to earn more income by publishing articles on Toutiao today! 1. Activate basic rights and interests: original articles can earn profits by advertising, and videos must be original in horizontal screen mode to earn profits. 2. Activate the rights of 100 fans: if the number of fans reaches 100 fans or above, you can get profits from micro headlines, original Q&amp;A creation and Q&amp;A. 3. Insist on original works: Original works include articles, micro headlines, questions, etc., and are required to be more than 300 words. Please note that if illegally plagiarized works are published as original works, credit points will be deducted, and even any profits will be deducted. 4. Verticality: When writing articles in professional fields, you cannot write articles across fields at will. You will not get appropriate recommendations, you will not be able to achieve the professionalism and refinement of your work, and it will be difficult to attract fans and readers. 5. Activity: high activity,

What is sudo and why is it important? What is sudo and why is it important? Feb 21, 2024 pm 07:01 PM

sudo (superuser execution) is a key command in Linux and Unix systems that allows ordinary users to run specific commands with root privileges. The function of sudo is mainly reflected in the following aspects: Providing permission control: sudo achieves strict control over system resources and sensitive operations by authorizing users to temporarily obtain superuser permissions. Ordinary users can only obtain temporary privileges through sudo when needed, and do not need to log in as superuser all the time. Improved security: By using sudo, you can avoid using the root account during routine operations. Using the root account for all operations may lead to unexpected system damage, as any mistaken or careless operation will have full permissions. and

Clock app missing in iPhone: How to fix it Clock app missing in iPhone: How to fix it May 03, 2024 pm 09:19 PM

Is the clock app missing from your phone? The date and time will still appear on your iPhone's status bar. However, without the Clock app, you won’t be able to use world clock, stopwatch, alarm clock, and many other features. Therefore, fixing missing clock app should be at the top of your to-do list. These solutions can help you resolve this issue. Fix 1 – Place the Clock App If you mistakenly removed the Clock app from your home screen, you can put the Clock app back in its place. Step 1 – Unlock your iPhone and start swiping to the left until you reach the App Library page. Step 2 – Next, search for “clock” in the search box. Step 3 – When you see “Clock” below in the search results, press and hold it and

Can't allow access to camera and microphone in iPhone Can't allow access to camera and microphone in iPhone Apr 23, 2024 am 11:13 AM

Are you getting "Unable to allow access to camera and microphone" when trying to use the app? Typically, you grant camera and microphone permissions to specific people on a need-to-provide basis. However, if you deny permission, the camera and microphone will not work and will display this error message instead. Solving this problem is very basic and you can do it in a minute or two. Fix 1 – Provide Camera, Microphone Permissions You can provide the necessary camera and microphone permissions directly in settings. Step 1 – Go to the Settings tab. Step 2 – Open the Privacy & Security panel. Step 3 – Turn on the “Camera” permission there. Step 4 – Inside, you will find a list of apps that have requested permission for your phone’s camera. Step 5 – Open the “Camera” of the specified app

Analysis of user password storage mechanism in Linux system Analysis of user password storage mechanism in Linux system Mar 20, 2024 pm 04:27 PM

Analysis of user password storage mechanism in Linux system In Linux system, the storage of user password is one of the very important security mechanisms. This article will analyze the storage mechanism of user passwords in Linux systems, including the encrypted storage of passwords, the password verification process, and how to securely manage user passwords. At the same time, specific code examples will be used to demonstrate the actual operation process of password storage. 1. Encrypted storage of passwords In Linux systems, user passwords are not stored in the system in plain text, but are encrypted and stored. L

See all articles
";
echo "The number of people online now is: $num";
echo "
User: $name