Log in

View Full Version : Counter?



alexjewell
01-14-2007, 02:10 AM
I have a client who wants the counter on their site to start at 3838. Well, I decided instead of figuring out a way to have the counter I already have start at 3838, I'd just get all of my myspace friends and I to refresh until we got that many.

Well, we were almost to 2000 when the script suddenly restarted.
It was so weird.
And I have NO idea why it did that.

Here's the script:



$file = fopen("counter.txt", "r");
$num = fgets($file,3838);
$num += 1;
fclose($file);

$file2 = fopen("counter.txt", "w");
fputs($file2, $num);
fclose($file2);

$num = $num/2;
$num = ceil($num);

$visits = '<i>'.$num.'</i> visits.<br />';
echo $visits;


Then, I just include that file where I want the number of visits.

counter.txt just has "3838" in it, because I wondered if putting the number I wanted to start with in there would work.
It didn't work.

If any of you has another script, or a way to make this one work...help?

There are also some quirks:

For some reason, everytime it was adding a visit, it was adding 2 instead. So I divided $num by 2.
Then, when it was doing that, eventually it was having numbers with decimals...
So that's why I did the ceil($num) part

apollo
01-14-2007, 03:13 AM
I notice you have your target conter value below
"$num = fgets($file,3838);"
That should not make a difference, as that number is being used as the number of bytes to return.

However, what about the following 2 lines:
$num = $num/2;
$num = ceil($num);

I don't think you need these, unless you only want the counter value to be half of what it really is?

I set the "counter.txt" to 3838 and removed the above lines and it seems to work fine.

So I would say just set the counter.txt to 3838 (ftp to server) and rem out the two lines in question and it should work.

alexjewell
01-14-2007, 03:23 AM
I explained those two lines above:



For some reason, everytime it was adding a visit, it was adding 2 instead. So I divided $num by 2.
Then, when it was doing that, eventually it was having numbers with decimals...
So that's why I did the ceil($num) part

apollo
01-14-2007, 03:30 AM
Sorry, I missed that,
I am using it like this and it seems to be consistant...


<?php
$file = fopen("counter.txt", "r");
$num = fgets($file,3838);
$num += 1;
fclose($file);

$file2 = fopen("counter.txt", "w");
fputs($file2, $num);
fclose($file2);

//$num = $num/2;
//$num = ceil($num);

$visits = '<i>'.$num.'</i> visits<br />';

echo $visits;
?>

it's at http://debbiemerrillshow.com/counter.php

alexjewell
01-14-2007, 04:06 AM
Hmm, that's so weird. For some reason, it was about seven thousand something when I included the script...
Then, when I just put the script in there, it was the right number.
Dude, that's so weird.

Well, thanks.
It's working now.
So that means a happy client.
And we like those.

apollo
01-14-2007, 04:12 AM
Here Is The Counter I Use
This COUNTER script only increments once per IP address and only on the INDEX page. If you include your IP address it will not count you.
Here is a FUNCTION I use for a page counter in all my scripts.



/*
****************************************
Function page_hits
****************************************/
#----- http://www.iwcomm.com/ -----#
function page_hits(){

#-----Finds Path To File-----#
@session_start();
$exp=explode('/',$_SERVER['SCRIPT_FILENAME']);
$ref='';
$n=4;
while(!strpos($exp[$n],'php')){
$ref.= '../';
$n++;
}
#-----For Testing -----#
//echo 'site_root = '.$_SERVER["DOCUMENT_ROOT"].'/include/hitcounter.dat<br />';
//$COUNT_FILE = $ref."include/hitcounter.dat";

#-----Path To Data File-----#
$COUNT_FILE = $_SERVER["DOCUMENT_ROOT"]."/inc/hitcounter.dat";

#-----Make Sure Data File Exists-----#
if (file_exists($COUNT_FILE)){
#-----Opens The Data File And Gets Current Value-----#
$fp = fopen("$COUNT_FILE", "r+");
flock($fp, 1);
$count = fgets($fp, 4096);
#-----Check To See If It Is OK To Increment-----#
if(($_SERVER['REMOTE_ADDR'] != $_SESSION['SERVER_IP'] && $_SERVER['REMOTE_ADDR'] != $_SESSION['CURRENT_IP']) && ($_SESSION['PAGE']=='/index.php')){
#-----Increment The Data File-----#
$count += 1;
#-----Set The Current Users IP Address-----#
$_SESSION['CURRENT_IP'] = $_SERVER['REMOTE_ADDR'];
}
#-----Update And Close The Data File-----#
fseek($fp,0);
fputs($fp, $count);
flock($fp, 3);
fclose($fp);
}else{
#-----Set Displayed Counter Value If An Error Occured-----#
$count = 'Many';
}
#-----Set Count Session Value And Return $count Values
$_SESSION['count'] = $count;
return $count;
}




Variables Used:

$COUNT_FILE - Name of the counter data text file (chmod 666) on server
$_SERVER["DOCUMENT_ROOT"] - Your server base path
$_SERVER['REMOTE_ADDR'] - IP address of user connecting to your server
$_SESSION['SERVER_IP'] - Your local IP address (when connecting to the server)
$_SESSION['CURRENT_IP'] - Set by script to prevent duplicate count per session
$_SESSION['PAGE'] - Will only count hits on this page
$_SESSION['count'] - Current counter value

Usage:
I call it from a page "footer" file, so it appears on all pages.


<?page_hits()?><?=COUNT_SITE_HAS?> <?=$_SESSION['count']?> <?=COUNT_VISITORS?>

Looks Like:
This Site Has Had 244 Visitors Since January 1st, 2007

apollo
01-14-2007, 04:15 AM
Great...
I just posted a smarter counter!

Good Luck!

alexjewell
01-14-2007, 04:15 AM
Alright, I may use yours in the future.
Thanks.