Thủ thuật sử dụng máy tính: Hiển thị số người online trên một trang web - Pdf 19

Hiển thị số người online trên một trang web
Để hiển thị số người đang online trên một trang web, chúng ta chỉ cần sử
dụng đối tượng Application và đối tượng Session để đếm số người. Biến
"visitors" sẽ được sử dụng để khởi tạo và đếm. Khi một user vào thăm trang
web, đối tượng session sẽ tăng lên một, khi user nào đó rời trang web,
session sẽ giảm đi một. Biến "visitors" cùng với các sự kiện của đối tượng
App và Session sẽ được đặt trong file global.asa.
file global.asa
Code:
<Script language = "vbscript" runat = "server">
Sub Application_OnStart
Application("visitors") = 0
End Sub
Sub Session_OnStart
Application.Lock
Application("visitors") = Application("visitors") + 1
Application.Unlock
End Sub
Sub Session_OnEnd
Application.Lock
Application("visitors") = Application("visitors") - 1
Application.Unlock
End Sub
</Script>
Để hiển thị số người hiện tại thăm trang web trong một file ASP:
Code:
<html>
<head></head>
<body>
<p>Có <% = response.write "Application("visitors")" %> đang online.</p>
</body>

function getNumber() {
return $this->numberOfUsers;
}
function printNumber() {
if($this->numberOfUsers == 1) {
echo "$this->numberOfUsers User online";
} else {
echo "$this->numberOfUsers Users online";
}
}
function refresh() {
global $REMOTE_ADDR, $PHP_SELF;
$currentTime = time();
$timeout = $currentTime - $this->timeoutSeconds;
mysql_connect($this->host, $this->user, $this->password)
or die('Error conecting to database');
mysql_db_query($this->database,
"INSERT INTO usersonline VALUES
('$currentTime','$REMOTE_ADDR','$PHP_SELF')")
or die('Error writing to database');
mysql_db_query($this->database,
"DELETE FROM usersonline WHERE timestamp <
$timeout")
or die('Error deleting from database');
$result = mysql_db_query($this->database,
"SELECT DISTINCT ip FROM usersonline WHERE
file='$PHP_SELF'")
or die('Error reading from database');
$this->numberOfUsers = mysql_num_rows($result);
mysql_close();


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status