473,320 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Code to calculate number of visitors

Hi there,

I developed a PHP code calculating the number of visits of a web site based on the more-or-less common 30-minutes approach. But meanwhile, the log-database got so huge, that it takes hours and hours to go through the log. I am sure this is due to an inefficient way to program the code.

Does anyone have a nice piece of code at hand or know where to look for that algorythm/code?

Thanks for any advice.
Jun 18 '07 #1
1 6939
Atli
5,058 Expert 4TB
I usually record the IP of visitors, and match it against a database log table, if it hasn't been counted for an x ammount of time, I add a row.
Once I've done that I create a session variable which is checked every time a user navigates on my web, so I wont be querying the database every time.

Example:
Expand|Select|Wrap|Line Numbers
  1. // Start session
  2. session_start();
  3.  
  4. // Connect SQL
  5. include("myDb.php");
  6. myDb->connect();
  7.  
  8. // Get the ammount of visits
  9. $res = myDb->fetch("SELECT COUNT(logID) AS 'count' FROM logTb");
  10. echo "Total visitors: ". $res[0]['count'];
  11.  
  12. // Check if you have been counted this session
  13. if(!isset($_SESSION['HasBeenCounted']))
  14. {
  15.   // Fetch the ammount of times
  16.   // this IP has been counted today
  17.   $IP = $_SERVER['REMOTE_ADDRESS'];
  18.   $res = myDb->fetch("SELECT logID FROM logTbl WHERE IP = '$IP' AND logDate = NOW()");
  19.  
  20.   // If it has not been counted; add it
  21.   if(count($res) == 0) {
  22.     myDb->query("INSERT INTO logTbl(IP, logDate) VALUES('$IP', NOW())");
  23.   }
  24.  
  25.   // Set the $_SESSION var
  26.   $_SESSION['HasBeenCounted'] = true;
  27. }
  28.  
  29.  
Jun 18 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Terry | last post by:
From the packet data returned from a query, where in the packet data are the bytes signifying the number of rows returned? Thanks, Terry
3
by: Frederik Sørensen | last post by:
I'm trying to do this calculation in xsl but i get an error message: "expression is not a node set" <xsl:variable name="height" select="round(130/width*height)+10"/> How do i save the result...
2
by: Rimdur | last post by:
I am trying to get a difference between two dates calculated in a query. I have Sales_Date as the first one, and Date() as the second one. How would I put the difference of these two days into a...
3
by: George | last post by:
Can someone tell me how to calculate the number of weeks between two dates. Your help is appreciated. Thanks, George
7
by: P. Schmidt-Volkmar | last post by:
Hi there, I have a string in which I want to calculate how often the character ';' occurs. If the character does not occur 42 times, the ";" should be added so the 42 are reached. My...
5
by: Beemer Biker | last post by:
I cant seem to get that date into any DateTime to make my calculation directly by subtracting "01-01-0000" from "now". After reading this:...
4
by: shilpareddy2787 | last post by:
Hello, I have some total values, I want to calculate percenatge of these Total Values. I want to divide the total with No. Of working Days Excluding Saturdays and Sundays in a given period. ...
1
by: AXRabbit | last post by:
Hi guys, I am currently doing a Car rental website project. Like my Car collection and return date is like in the website. http://www.avis.com/car-rental/avisHome/home.ac Well i need a function or...
6
by: sarfraznawaz | last post by:
Hi, I am Web Designer. I am new to this coding related job. Can anyone help me how to sort this out. How to calculate number of words with given price? Thanks. Sarfraz
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.