Xem mẫu

  1. 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: 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
  2. Application.Lock Application("visitors") = Application("visitors") - 1 Application.Unlock End Sub Để hiển thị số người hiện tại thăm trang web trong một file ASP: Code: Có đang online. _____________________________________________________________ Hiển thị số người online trên trang. Tôi có được một đoạn code, cũng hay lắm. Các bạn có thể sử dụng lớp này vào việc hiển thị người online khi duyệt site của mình. Trước tiên, cần tạo một bảng trong cơ sở dữ liệu Trích dẫn: CREATE TABLE usersonline ( timestamp int(15) DEFAULT '0' NOT NULL,
  3. ip varchar(40) NOT NULL, file varchar(100) NOT NULL, INDEX (timestamp), INDEX ip(ip), INDEX file(file) ); Sau đấy các bạn tạo một file php để lưu trữ một lớp, lớp này sẽ được sử dụng để hiển thị số người online. file đó gọi là usersOnline.php Mã PHP:
  4. 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');
  5. 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(); } } ?> Còn trong file cần hiển thị, các bạn chỉ cần tạo một thể hiện của nó, vậy là được Mã PHP:
  6. $ol->printNumber(); ?> Đoạn code này tôi đã test chiều nay rồi, hiển thị đúng số người online. Designed by FSOSR. Email : fsosr2005@yahoo.com.vn
nguon tai.lieu . vn