0

hello there thanks for help mke in many questions, I've learened a lot from this great community
I have question about creating visitor counter,daily,monthly,yearly
I've created a counter depending on IP address but all my costomer are in the same IP so this counter will not be accurate, So I used session ID as an Identifire, so every visitor enter the site a session will created and saved with time into database, and the total of these sessions will be the counter for visitors

  • I want to know how to seperate daily visitor from monthly ?
  • I've created a second table to store final result so I can clear the visitors table.

visitor table

session_id -- time

here is the results for saving
e46266109ac18845fc35bc373397d2b0

and this code I used for saving

<?php
session_start(); 
$session_id = session_id();
counters();
function counters() {
    global $session_id;
    $time = time();
    $num = mysql_num_rows(mysql_query("SELECT * FROM online_visitors WHERE session_id='{$session_id}' LIMIT 1"));
    if($num != 1){
    $sql = "INSERT INTO online_visitors VALUES('{$session_id}','{$time}')";
    $query = @mysql_query($sql) or die("Error");
    }
}
3
Contributors
4
Replies
33
Views
4 Years
Discussion Span
Last Post by OsaMasw
0

Have you taken into account the fact that the same visitor will have different session ID on each visit (on each session)?

0

yes that came in my mind, but the session expire in 30 minutes so it will not affect the counter in a bad way :D,

1

yes that came in my mind, but the session expire in 30 minutes so it will not affect the counter in a bad way :D,

You think? I use some sites some 10 times a day. Often on different machines. I don't think there's an easy solution to this though, unless you force login.

It seems that you query the DB at every page impression. This can be wasteful. You could just store a session var to say whether that session has been logged or not, e.g.

//place the code below in an include file so that it can be accessed from every page
session_start();
if(!isset($_SESSION['logged'])){
    //do your insert into db stuff here
    $_SESSION['logged'] = 1;
}

Edited by diafol

0

thanks I found a way to create daily counter and total counter

<?php
session_start();
if(!isset($_SESSION['logged'])){
$ip = session_id(); // this is in case I want to use session ID
$ip = $_SERVER['REMOTE_ADDR']; // this is in case I want to use IP
counter();
$_SESSION['logged'] = 1;
}
?>

<?php
function counters() {
    global $ip;
    $date = date("m d, Y");
 $checkip = "0";
 $checkdate = "0"; 


 $results= mysql_query("SELECT time FROM visitors")
or die(mysql_error());

  while ( $row = mysql_fetch_array($results) ) { 
  $cdate = $row[time]; 

  if ($cdate == $date) {
  $checkdate = "$checkdate + 1"; 
  }}

   if ($checkdate == "0") {  

  mysql_query("DELETE FROM visitors");
  mysql_query("UPDATE `counter` SET today='1', total = total + 1");
  mysql_query("INSERT INTO `visitors` SET ip='$ip' , time='$date'");

}

else

$results= mysql_query("SELECT ip, time FROM visitors")
or die(mysql_error());

  while ( $row = mysql_fetch_array($results) ) { 
  $cip = $row[ip];

  if ($cip == $ip) {
  $checkip = "$checkip + 1"; 
  }} 

   if ($checkip == "0") {
   {
   if (mysql_result($results, 0, 'time') == $date ){
   mysql_query("UPDATE counter SET today = today + 1 , total = total + 1");
   mysql_query("INSERT INTO `visitors` SET ip='$ip' , time='$date'");
   } 
} 
  }
}

?>

Edited by OsaMasw

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.