sim_tcr
Posts: 326
Joined: Tue Nov 06, 2012 1:01 pm
Location: Bangalore
Contact: Website

php web hit counter question.

Fri Jan 04, 2013 1:52 pm

Hello,

I am running on raspbian wheezy, apache2 and php5.
I have implemented a php web counter at http://raspisimon.no-ip.org (right bottom of the page). Code is below,

counter.php

Code: Select all

<?php
$count_my_page = ("hitcounter.txt");
$hits = file($count_my_page);
$hits[0] ++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");
fclose($fp);
echo $hits[0];
?>
index.php

Code: Select all

<?php
echo "Total Visits:";
include ("counter.php");
?>
Is there a way to make the counter to record hits only from outside internet. In other words prevent counter from recording hits from (192.168.1.0 to 192.168.1.255)?
http://raspisimon.no-ip.org
Raspberry Pi Model B x 2, Raspberry Pi 2 x 2, Transcend 32GB Class 10, Transcend 16GB Class 10, Transcend 8GB Class 4, Custom 12V 1.5A (stepped down to 5.5V)

++bahjatk++
Posts: 36
Joined: Sun Dec 30, 2012 10:02 am

Re: php web hit counter question.

Fri Jan 04, 2013 2:30 pm

Code: Select all

$_SERVER['REMOTE_ADDR']
returns the IP of the client. You can do something like this :

Code: Select all

if (strrpos($_SERVER['REMOTE_ADDR'],"192.168.1")==0) {
//do not ++
} else {
$hits[0]++;
}

sim_tcr
Posts: 326
Joined: Tue Nov 06, 2012 1:01 pm
Location: Bangalore
Contact: Website

Re: php web hit counter question.

Sat Jan 05, 2013 4:21 am

++bahjatk++ wrote:

Code: Select all

$_SERVER['REMOTE_ADDR']
returns the IP of the client. You can do something like this :

Code: Select all

if (strrpos($_SERVER['REMOTE_ADDR'],"192.168.1")==0) {
//do not ++
} else {
$hits[0]++;
}
I added above code to my counter.php and it looks like below now,

Code: Select all

if (strrpos($_SERVER['REMOTE_ADDR'],"192.168.1")==0) {
//do not ++
} else {
$hits[0]++;
}
$count_my_page = ("hitcounter.txt");
$hits = file($count_my_page);
$hits[0] ++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");
fclose($fp);
echo $hits[0];
?>
Still when I browse from localhost it counts the hit.
http://raspisimon.no-ip.org
Raspberry Pi Model B x 2, Raspberry Pi 2 x 2, Transcend 32GB Class 10, Transcend 16GB Class 10, Transcend 8GB Class 4, Custom 12V 1.5A (stepped down to 5.5V)

efflandt
Posts: 359
Joined: Mon Dec 03, 2012 2:47 am
Location: Elgin, IL USA

Re: php web hit counter question.

Sat Jan 05, 2013 7:49 am

localhost (yourself) is not an IP that begins with 192.168. IP (that is a private LAN IP).

localhost is an IP that begins with "127." so you also want to avoid counting hits from that.

sim_tcr
Posts: 326
Joined: Tue Nov 06, 2012 1:01 pm
Location: Bangalore
Contact: Website

Re: php web hit counter question.

Sat Jan 05, 2013 7:57 am

efflandt wrote:localhost (yourself) is not an IP that begins with 192.168. IP (that is a private LAN IP).

localhost is an IP that begins with "127." so you also want to avoid counting hits from that.
Could you please modify my code above and reply?
http://raspisimon.no-ip.org
Raspberry Pi Model B x 2, Raspberry Pi 2 x 2, Transcend 32GB Class 10, Transcend 16GB Class 10, Transcend 8GB Class 4, Custom 12V 1.5A (stepped down to 5.5V)

++bahjatk++
Posts: 36
Joined: Sun Dec 30, 2012 10:02 am

Re: php web hit counter question.

Sun Jan 06, 2013 10:54 am

sim_tcr wrote: I added above code to my counter.php and it looks like below now,

Code: Select all

if (strrpos($_SERVER['REMOTE_ADDR'],"192.168.1")==0) {
//do not ++
} else {
$hits[0]++;
}
$count_my_page = ("hitcounter.txt");
$hits = file($count_my_page);
$hits[0] ++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");
fclose($fp);
echo $hits[0];
?>
Still when I browse from localhost it counts the hit.
you got it all wrong...
Code should like this :

Code: Select all

if (strrpos($_SERVER['REMOTE_ADDR'],"192.168.1")==0) {
//do not ++
} else {
$count_my_page = ("hitcounter.txt");
$hits = file($count_my_page);
$hits[0] ++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");
fclose($fp);
}
echo $hits[0];
?>
it still counts the hit because you placed your own hit counter outside the if... else... statement.

If you do that code and it stills counts the hit, try replacing the "192.168.1" part with "127.0.0" as efflandt suggested
efflandt wrote:localhost is an IP that begins with "127." so you also want to avoid counting hits from that.

sim_tcr
Posts: 326
Joined: Tue Nov 06, 2012 1:01 pm
Location: Bangalore
Contact: Website

Re: php web hit counter question.

Sun Jan 06, 2013 11:12 am

++bahjatk++ wrote: Code should like this :

Code: Select all

if (strrpos($_SERVER['REMOTE_ADDR'],"192.168.1")==0) {
//do not ++
} else {
$count_my_page = ("hitcounter.txt");
$hits = file($count_my_page);
$hits[0] ++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");
fclose($fp);
}
echo $hits[0];
?>
Tried above one, but nothing is counted either from outside or inside.
http://raspisimon.no-ip.org
Raspberry Pi Model B x 2, Raspberry Pi 2 x 2, Transcend 32GB Class 10, Transcend 16GB Class 10, Transcend 8GB Class 4, Custom 12V 1.5A (stepped down to 5.5V)

++bahjatk++
Posts: 36
Joined: Sun Dec 30, 2012 10:02 am

Re: php web hit counter question.

Sun Jan 06, 2013 12:55 pm

sim_tcr wrote:
++bahjatk++ wrote: Code should like this :

Code: Select all

if (strrpos($_SERVER['REMOTE_ADDR'],"192.168.1")==0) {
//do not ++
} else {
$count_my_page = ("hitcounter.txt");
$hits = file($count_my_page);
$hits[0] ++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");
fclose($fp);
}
echo $hits[0];
?>
Tried above one, but nothing is counted either from outside or inside.
Hmmm...
to test it out, do echo $_SERVER['REMOTE_ADDR'];
then replace the argument in the if like this :

Code: Select all

if ($_SERVER['REMOTE_ADDR']=="the output") {
where "the output" is a quote, followed by what you got from echoing $_SERVER['REMOTE_ADDR'], followed by another quote.

Return to “General discussion”