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
Re: How to put number of visitors till date in PHP
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
KrisSiegel.com - My Personal Website with my blog and portfolio Don't Forget to Rate Posts!
Re: How to put number of visitors till date in PHP
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:
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 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:
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");
/* 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);
?>
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:
number.php?number=1234567890
Will produce:
I've attached the image generation script and the image files containing the numbers.
Re: How to put number of visitors till date in PHP
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.
Re: How to put number of visitors till date in PHP
Originally Posted by kasracer
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
Hi
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
Re: How to put number of visitors till date in PHP
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.
KrisSiegel.com - My Personal Website with my blog and portfolio Don't Forget to Rate Posts!
Re: How to put number of visitors till date in PHP
Originally Posted by sinyal_varsha
Hi
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
An IP address is unreliable. You need to use a cookie. Look at my previous reply.
Re: How to put number of visitors till date in PHP
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.
Re: How to put number of visitors till date in PHP
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.
Re: How to put number of visitors till date in PHP
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.
Re: How to put number of visitors till date in PHP
This topic should be put into a debate forum.Cuz thats what its turnin into.
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
Re: How to put number of visitors till date in PHP
Originally Posted by penagate
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.
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.