Link to home
Start Free TrialLog in
Avatar of aplelois
aplelois

asked on

counter

hello,
I have this counter that writes a number to a txt file everytime someone visits my site.
I would like to write the number + ip address.. and block my ip address from counting everytime I visit it...
can someone help me with this?

my code!

index.php
<?include ("counter.php");?>

counter.php
<?
$TextFile = "hits.txt";
$Count = trim(file_get_contents($TextFile));


$FP = fopen($TextFile, "r");
$Count=fgets($FP, 4096);
fclose ($FP);

settype($Count, "integer");
$Count++;

if ($FP = fopen ($TextFile, "w")){
  fwrite ($FP, $Count);
  fclose ($FP);
}

echo($Count);
?>
ASKER CERTIFIED SOLUTION
Avatar of Harisha M G
Harisha M G
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aplelois
aplelois

ASKER

thanks mgh_mgharish, but this is not working!!

<?
$TextFile = "hits.txt";
$Count = trim(file_get_contents($TextFile));


$FP = fopen($TextFile, "r");
$Count=fgets($FP, 4096);
fclose ($FP);

settype($Count, "integer");
if($_SERVER['REMOTE_ADDR'] == "127.0.0.1")        // Change to your IP
    $Count++;

if ($FP = fopen ($TextFile, "w")){
  fwrite ($FP, $Count);
  fclose ($FP);
}

echo($Count);
?>
Did you change your IP there ?
its supposed to wite the number of hits in hits.txt and the number of ips in ips.txt
and block my ip from the counter!
Yes it does that only, and for that, it should know YOUR IP, since it should count others.
Just change 127.0.0.1 to your IP.

You can see your actual IP using "ipconfig" command (if you are using Windows)
yup, I got my IP this ways

<?
$ipaddr = $HTTP_SERVER_VARS["REMOTE_ADDR"];
echo $ipaddr
?>
so any idea what might be the problem ?
Are you having static ip or dynamic ip ?
its been static for the last 3yrs
You need to put your IP inside that code. Do NOT use $HTTP_SERVER_VARS["REMOTE_ADDR"];
ok now its working.... I had to change this
if($_SERVER['REMOTE_ADDR'] ==
to
if($_SERVER['REMOTE_ADDR'] !=
now how do I add a section to write the ips in a text file called ips.txt??
Oh yeah.. I should have put it the other way :D
It will be logged in your server. Why do you want another file for that ?
cause im testing some things in my website, I only need page1.php to log the ips no other page!!

page1.php
<?include ("counter.php");?>
Well, for that, within that if statement, open a file for appending, and put the ip address..

if($_SERVER['REMOTE_ADDR'] != "$yourip")
{
  $counter++;
  // open file
  // put the new ip
  // close file
}
well I coded this but is not working

<?
$HitsFile = "hits.txt";
$IPsFile = "ips.txt";
$Count = trim(file_get_contents($HitsFile));


$FP = fopen($HitsFile, "r");
$Count=fgets($FP, 4096);
fclose ($FP);

$myip = $_SERVER['REMOTE_ADDR'];

settype($Count, "integer");
if($_SERVER['REMOTE_ADDR'] != "1.2.3.4")
{
  $counter++;
  fopen($IPsFile, "r");
  fwrite ($myip);
  fclose ($IPsFile);
}

if ($FP = fopen ($HitsFile, "w")){
  fwrite ($FP, $Count);
  fclose ($FP);
}

echo ($Count);
?>
Warning: Wrong parameter count for fwrite() in /home/virtual/site13/fst/var/www/html/counter/counter.php on line 19
Warning: fclose(): supplied argument is not a valid stream resource in /home/virtual/site13/fst/var/www/html/counter/counter.php on line 20
 fwrite ($IPsFile, $myip);
Warning: fwrite(): supplied argument is not a valid stream resource in /home/virtual/site13/fst/var/www/html/counter/counter.php on line 18
Warning: fclose(): supplied argument is not a valid stream resource in /home/virtual/site13/fst/var/www/html/counter/counter.php on line 19
Try this:

if($_SERVER['REMOTE_ADDR'] != "1.2.3.4")
{
  $counter++;
  $myfile = fopen("ips.txt", "a");
  fwrite ($myfile, "$myip\n");
  fclose ($myfile);
}
ok now its writing the ip but its not writing the hits and if i put my ip it still write the ips

<?
$HitsFile = "hits.txt";
$Count = trim(file_get_contents($HitsFile));

$FP = fopen($HitsFile, "r");
$Count=fgets($FP, 4096);
fclose ($FP);

$myip = $_SERVER['REMOTE_ADDR'];

settype($Count, "integer");
if($_SERVER['REMOTE_ADDR'] != "71.196.5.63")
{
  $counter++;
  $myfile = fopen("ips.txt", "a");
  fwrite ($myfile, "$myip\n");
  fclose ($myfile);
}

if ($FP = fopen ($HitsFile, "w")){
  fwrite ($FP, $Count);
  fclose ($FP);
}

echo ($Count);
?>
Shouldn't that be $Count++ instead of $counter++ ?
yes, thats what I was looking for. thank you very much sir!
Glad I could help !

Thanks for the grade :)