Hi
I am making a PHP poject in that i have to put counter ie. no. of visitors till date in PHP .I am not able to find exact method for that can anyone help me
Thank You
Printable View
Hi
I am making a PHP poject in that i have to put counter ie. no. of visitors till date in PHP .I am not able to find exact method for that can anyone help me
Thank You
How are you tracking visitors? Everytime someone requests a PHP page, you could just INSERT into a MySQL table the IP and Date or something so you can track the visitor. Or maybe write to a file.
Then you'd just count the rows and print the number of visitors since date("m-d-Y") or something.
We need more information about what you want
You can count unique visits and repeat visits. You can count repeat visits easily just bycount the number of page requests. For unique visits, place a cookie on the users computer and use it to check whether the visit has been logged in the past.
This is a script I use:
It requires a database with the following table:PHP Code:<?php
$db = new DB;
if (isset($_COOKIE['visit'])) {
$id = (int) $_COOKIE['visit'];
$query = "SELECT id FROM hits WHERE id='$id'";
$result = $db->query($query);
if(($row = @$db->fetch_assoc($result))) {
update_hit($row['id']);
} else {
create_hit();
}
} else {
create_hit();
}
header('Content-Type: image/gif');
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header('Pragma: private');
@readfile('1px.gif');
function create_hit()
{
global $db;
$last_visit = time();
$ip = ip_to_int($_SERVER['REMOTE_ADDR']);
$expire = mktime(0,0,0,12,31,2007);
$insert = "INSERT INTO hits (ip, last_visit) VALUES ($ip, $last_visit)";
$db->query($insert);
$id = $db->last_insert_id();
setcookie('visit', $id, $expire);
}
function update_hit($id)
{
global $db;
$id = (int) $id;
$last_visit = time();
$update = "UPDATE hits SET last_visit=$last_visit,visit_count=visit_count+1 WHERE id =$id";
$db->query($update);
}
function ip_to_int($ip)
{
$parts = explode('.', $ip);
$ip_int = 0;
for($x= 0; $x <= 3; $x++) {
$multiplier = pow(256, $x);
$ip_int += ($multiplier * $parts[3 - $x]);
}
return $ip_int;
}
?>
You also need an image called 1px.gif. Insert this into your page as a hidden image:Code:CREATE TABLE hits (
id INTEGER PRIMARY KEY,
hash VARCHAR(32) NOT NULL,
ip INTEGER NOT NULL,
last_visit INTEGER,
visit_count INTEGER NOT NULL);
If you want to show a counter, you can replace the image with a dynamically generated image. This script will produce an image containg the number you require:HTML Code:<img src="1px.php" alt="counter" style="display: none" />
You need to have the GD extension enabled to use it. I've a attached the numbers as a download. The link is constructed with a number in the querystring, so:PHP Code:<?php
/* the following headers prvent caching by the browser */
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
// HTTP/1.0
header("Pragma: no-cache");
$number = (string) ((int)@$_GET['number']);
/* how many digits do we need - calculate the length of the image */
$imageLength = strlen($number) * 16;
/* create an empty image, big enough to hold the required number of 16x24 digits */
$image = imagecreate($imageLength, 24);
/* an empty array to store already loaded image resources */
$img = array();
/* loop through each character in the string of numbers */
for($x = 0; $x < strlen($number); $x++) {
/* get the number, locate its position in the big image */
$no = substr($number, $x, 1);
$xStart = ($x==0)?0:($x * 16) + 1;
/* if this number isn't already loaded, load it */
if(! is_resource(@$img[$no])) {
$img[$no] = @imagecreatefromgif("num/$no.gif");
}
/* add the appropriate digit to the big image */
imagecopy($image, $img[$no],
$xStart,0,0,0,16,24);
}
/* image - flush to the browser */
header('Content-Type: image/gif');
imagegif($image);
?>
number.php?number=1234567890
Will produce:
http://adam.codedv.com/examples/img_...ber=1234567890
I've attached the image generation script and the image files containing the numbers.
I have a very very simple script going on my index page. Every load it opens and increments a text file. this is not the most accurate since simply reloading the page will increment it. I plan to update it to a better tracker later but its a very basic one, not that much code.
HiQuote:
Originally Posted by kasracer
Thanks for your great response!
what exactly i want is that as the website page opens no. of visitors increases by one by on refreshing it should not increase .
Any such file is there in php that call at one time only .
I think it is not an good idea to keep track of visitors in dbase as dbase will soon become very heavy ? What do u think ? Give me some other options
Since you want to make it so hitting refresh doesn't increase the number of hits, you'd have to keep track of IP addresses. You could try added all of these visits into a text file but that would become large and slower to parse. It would probably be most efficient to use a database and store the visits and IP addresses and don't add anything to the database if the IP address already exists.
An IP address is unreliable. You need to use a cookie. Look at my previous reply.Quote:
Originally Posted by sinyal_varsha
Cookies can be unreliable too since not all browser supports then, and not everyone have them turned on. I was in a big presentation at the UN building about how to track users and count them ++. And the big companies said they used a mix between cookies and an advanced version of the "text file" method. No matter how much time you lie in this, it will never be perfect.
- ØØ -
You could count sessions. Not the same as unique visitors, but it is a "unique visit" if you like. Just store a session var, and increment the visit count if the var is not present.
Again though. Sessions rely on cookies.
The only truely reliable way of doing it is askling users to make an accoutn and login to use the site. However, you'll then have usability issues.
Yes, but a session times out. Like I said, you're then not counting unique VISITORS, you're counting unique VISITS. Since it's utterly impossible to count unique visitors.
This topic should be put into a debate forum.Cuz thats what its turnin into.
Sessions are a little overkill for hit counting. But with a session you can opt not to use cookies and instead append the SID constant to all your links.Quote:
Originally Posted by penagate
Plague, your signature offends me. Go away.