...

View Full Version : Getting vistors from the previous day.



SDP2006
10-27-2003, 10:22 PM
How can I get the number of visitors per day and then write "$NUMBER visitors yesterday"?

Thanks

mordred
10-27-2003, 10:26 PM
You mean from Apaches logfile?

SDP2006
10-27-2003, 10:29 PM
I don't really know. I just want to get the number of visitors from the day and then on the next day right "There were $number visitors yesterday"

coffeedemon
10-27-2003, 10:54 PM
you gunna use a database of some sort or do you want it to magicaly apear on its own?

SDP2006
10-27-2003, 11:39 PM
No reason to be a smart alec about it.

mordred
10-27-2003, 11:49 PM
Here's a quick hack that stores a count of visitors for each day as a serialized array in a file and reads from it the number for yesterdays visits:



session_start();

function logVisit() {
// check whether the visitor has been tracked
if (isset($_SESSION['visited'])) {
return false;
}

$filename = 'visits.log';
$visits = array();
$today = date('m/d/Y');

// first open the file and get the array...
$content = implode('', file($filename));
if (strlen($content) > 0) {
$visits = unserialize($content);
}

// check if a key for today exists, increment that value
if (isset($visits[$today])) {
$visits[$today] += 1;
} else {
$visits[$today] = 1;
}

// store the array in the logfile again
$fp = fopen($filename, 'w');
fwrite($fp, serialize($visits));
fclose($fp);

// mark visitor as tracked by putting a flag into the session
$_SESSION['visited'] = true;
return true;
}

function getVisitorCount($timestamp) {
$filename = 'visits.log';
$visits = array();

// open the file, get the array
$content = implode('', file($filename));
if (strlen($content) > 0) {
$visits = unserialize($content);
}

// build date-key
$day = date('m/d/Y', $timestamp);

// return the number of visitors for a day
return isset($visits[$day]) ? ($visits[$day]) : (0);
}

logVisit();

$number = getVisitorCount(strtotime('yesterday'));
print "There were $number visitors yesterday";


Of course you need to include the session part and logVisit() on any page that could/should be reached of the visitor. And after a year or so the logfile might have swelled to 20kB so you might truncated it a little. Set the file permissions for visits.log as it fits your server environment.

coffeedemon
10-28-2003, 12:04 AM
Wasn't supposed to be taken as offensive .

but anyway. do you want this for a personal log?

You can like the above store them in a file or you can use a database and use sessions so you can actual visists rather then hits.

SDP2006
10-28-2003, 12:26 AM
So, mordred, I just need to create visits.log and include it in my index.php page?

Is that all I need to do?

Thanks

mordred
10-28-2003, 12:38 AM
You need to create visits.log in the same directory as index.php, make visits.log writable, and put the PHP code above into your index.php. That's all.

SDP2006
10-28-2003, 01:13 AM
All is well mordred except for this --

Warning: Cannot send session cookie - headers already sent by (output started at /usr/local/psa/home/vhosts/net-riches.com/httpdocs/includes/index.php:9) in /usr/local/psa/home/vhosts/net-riches.com/httpdocs/includes/visits.php on line 2

Warning: Cannot send session cache limiter - headers already sent (output started at /usr/local/psa/home/vhosts/net-riches.com/httpdocs/includes/index.php:9) in /usr/local/psa/home/vhosts/net-riches.com/httpdocs/includes/visits.php on line 2


Know why?

mordred
10-28-2003, 01:35 AM
You can't have HTML output before the session is started. Move session_start() up in your file that it appears before any HTML (or even a single line break!).

SDP2006
10-28-2003, 02:04 AM
Yay! No more errors! Is it supposed to say "There were 0 visitors yesterday?"

Thanks

mordred
10-28-2003, 11:50 AM
What did you expect? The code just tracks visitors from the time it was installed, and since you did not have it installed yesterday, you can't have any entries for yesterdays visitors... ;)

Caffeine
10-28-2003, 01:15 PM
Sorry... but loool :D

I mean no harm, but I find the last 2 replies amusing :p

SDP2006
10-28-2003, 01:32 PM
Here it is --
http://www.net-riches.com/includes/index.php?cat=home

Bottom right hand corner under "Powered by PHP" image....



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum