09/10/2018, 18:08
[Q] Đếm số người đang truy cập trang web
Tôi đang thiết kế một website dùng php, Mysql, Tôi muốn biết số người đang truy cập vào trang web của tôi , ai biết chỉ giúp , Cám ơn trước nhé.
Bài liên quan
<!----------- save this file as count.php ------------>
<?php
/* +------------------------------------------------------------+
Page Counter v1.5.0 Filename: count.php
Copyright 1998-2001 Telematics Software
All Rights Reserved
Author: mr.shifter@hosted.uklinux.net
http://www.hosted.uklinux.net/php/freescripts/index.php
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed WITHOUT ANY WARRANTY of
MERCHANTABILITY or FITNESS FOR ANY PARTICULAR PURPOSE.
Your usage of this programme is entirely at your own risk
Make a text file called 'count.txt' place it in the same
directory as this php script. On unix you will need to
chmod 'count.txt' to 666 for read/ write permissions
Make another directory call it 'images' and place in here
your graphical image files numbered from 0.gif to 9.gif
+-----------------------------------------------------------+ */
/* Set Necessary Variables: */
$counterfile= "./count.txt"; #absolute path/directory as required
$imagedir= "./images"; # absolute path/directory as required No trailing slash
$type=1; # 1=graphic display, 0= text display
$ext=".gif"; # use .gif, .jpeg or .png as needed
/* End Variables section */
if(! ($fp = fopen($counterfile, "r"))) die ("Can not open $counterfile.");
$counter = (int) fread($fp, 20);
fclose($fp);
$counter++;
$image_tag_str = ""; // Leave empty
for ($i=0; $i < strlen($counter); $i++)
{
$image_src = $imagedir."/".substr($counter, $i, 1) .$ext;
$image_tag_str .="<IMG SRC=\"$image_src\" BORDER=\"0\">";
}
// then display the graphic or text image
// to use a hidden counter comment out the next five lines
if($type==1){
echo("You Are Visitor No: $image_tag_str" );
}else{
echo("You Are Visitor No: $counter" );
}
if(!($fp= fopen($counterfile, "w"))) die ("Can not write to $counterfile");
fwrite($fp, $counter);
fclose($fp);
?>
<!---------- end of count.php ------------------------------>
<!---------- Save next file as count.txt -------------------->
<!-- Remember to chmod 666 (read/write all) count.txt --------->
75
<!--------------- End of count.txt ------------------------->
Good luck ! Saigon 75
session_start();
define("MAX_IDLE_TIME", 3);
function getOnlineUsers(){
if ( $directory_handle = opendir( session_save_path() ) ) {
$count = 0;
while ( false !== ( $file = readdir( $directory_handle ) ) ) {
if($file != '.' && $file != '..'){
if(time()- fileatime(session_save_path() . '\\' . $file) < MAX_IDLE_TIME * 60) {
$count++;
}
}
closedir($directory_handle);
return $count;
} else {
return false;
}
}
echo "Number of online users: " . getOnlineUsers() . "<br />";