Help! cần code đếm lượng khách đang xem website 2007-08-28 11:54:23
Chào mọi người.
Mình đang tìm hiểu PHP. Mình có dùng 1 đoạn code để đếm lượng khách đã viếng thăm website.
Nhưng mình không biết cách nào để đếm lượng khách đang xem.
Mong mọi người giúp đỡ.
Đây là đoạn code đếm lượng khách viếng thăm website:
<?
$CountFile = "index.log";
$CF = fopen ($CountFile, "r");
$Hits = fread ($CF, filesize ($CountFile));
fclose ($CF);
$Hits++;
$CF = fopen ($CountFile, "w");
fwrite ($CF, $Hits);
fclose ($CF);
echo ($Hits);
?>
Tra loi 6 comment(s) 2007-08-28 11:54:23
TG 2007-08-28 01:16:09
Bạn có thể dùng đoạn code sau để đếm số lượng đang truy cập:
<?
//copyright phpbasic.com
$CountFile = 'basic_visit.txt';
$now = strtotime('now');
$time = 30;
$file = file($CountFile);
$newfile = array();
for($i=0; $i<count($file);$i++){
if($file[$i] + $time > $now) $newfile[] = $file[$i];
}
print 'Onlines:'.(count($newfile)+1);
unlink($CountFile);
$CF = fopen ($CountFile, "a+");
$new = count($newfile)?implode("\n",$newfile)."\n".$now:$now;
fwrite ($CF,$new );
fclose ($CF);
?>
Ý tưởng:
- Mỗi khi truy cập thì lưu thời điểm hiện tại vào 1 file
- sau đó đọc file này và tìm ra những kết nối cách $now không quá $time (số người kết nối trong 1 khoảng thời gian $time)
Cách này chỉ tính được số lượng kết nối chứ không tính theo IP, tuy nhiên bạn có thể chỉnh sửa lại 1 tí để đếm được theo IP
Tra loi
datgs 2009-02-02 03:05:03
Kiểm soát lượt truy cập sử dụng SESSION.
Tra loi
gaulucky92 2009-02-02 03:39:48
Ừng dụng mở rộng: đếm số thành viên đang truy cập + danh sách những thành viên đó. Demo thì có thể xem ở các forum VBB, IPB... phần thống kê số thành viên.
Đây là code của tui:
>>> http://freecodevn.com/for@um/showpost.php?p=307610&postcount=4
Tra loi
TNguyễn 2009-04-08 10:38:20
$nowtime = time();
$time_update = $nowtime - 1200; // 20'
session_name("sid");
session_start();
session_register("start_time");
if ($_SESSION["start_time"]<$time_update)
{
$_SESSION["start_time"] = $nowtime;
$sess_id = session_id();
$prevtime = $nowtime - 1800;//30'
$dbsql = new db_mysql;
$dbsql->connect();
$dbsql->selectdb();
//-----------------------------------------------------
$sql_delete = "DELETE FROM ".$tbfix."_sessions WHERE start_time<".$prevtime;
$dbsql->query($sql_delete);
$sql_select = "SELECT * FROM ".$tbfix."_sessions WHERE session_id='".$sess_id."'";
$dbsql->query($sql_select);
if ($dbsql->num_rows()==0)
{
$sql_insert = "INSERT INTO ".$tbfix."_sessions(session_id,start_time) VALUES('".$sess_id."',".$nowtime.")";
$dbsql->query($sql_insert);
}
//-----------------------------------------------------
$dbsql->close();
}
Tra loi
Kay 2009-10-06 09:28:45
Tra loi
kenzero 2009-10-06 02:53:15
Đúng ý bạn nhé:
global $sql;
$tgnew = time() - 60;
$result = $sql->query("SELECT * from useronline WHERE useronline_ip = '".$_SERVER["REMOTE_ADDR"]."'");
if($sql->num_rows($result)){//Neu da ton tai ip
$sql->query("UPDATE useronline SET useronline_time = '".time()."' WHERE useronline_ip = '".$_SERVER["REMOTE_ADDR"]."'");
}
else{
$sql->query("INSERT INTO useronline (useronline_ip,useronline_time) VALUES ('".$_SERVER["REMOTE_ADDR"]."','".time()."') ");
}
$sql->query("DELETE FROM useronline WHERE useronline_time < ".(int)$tgnew."");
$result = $sql->query("SELECT useronline_id from useronline");
return $sql->num_rows($result);
}
Tra loi
Y kien