• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 305
  • Last Modified:

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);
?>
0
aplelois
Asked:
aplelois
  • 13
  • 13
1 Solution
 
Harisha M GCommented:
Hi, you need to know your IP address...

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




---
Harish
0
 
apleloisAuthor Commented:
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);
?>
0
 
Harisha M GCommented:
Did you change your IP there ?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
apleloisAuthor Commented:
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!
0
 
Harisha M GCommented:
Yes it does that only, and for that, it should know YOUR IP, since it should count others.
0
 
Harisha M GCommented:
Just change 127.0.0.1 to your IP.

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

<?
$ipaddr = $HTTP_SERVER_VARS["REMOTE_ADDR"];
echo $ipaddr
?>
0
 
apleloisAuthor Commented:
so any idea what might be the problem ?
0
 
Harisha M GCommented:
Are you having static ip or dynamic ip ?
0
 
apleloisAuthor Commented:
its been static for the last 3yrs
0
 
Harisha M GCommented:
You need to put your IP inside that code. Do NOT use $HTTP_SERVER_VARS["REMOTE_ADDR"];
0
 
apleloisAuthor Commented:
ok now its working.... I had to change this
if($_SERVER['REMOTE_ADDR'] ==
to
if($_SERVER['REMOTE_ADDR'] !=
0
 
apleloisAuthor Commented:
now how do I add a section to write the ips in a text file called ips.txt??
0
 
Harisha M GCommented:
Oh yeah.. I should have put it the other way :D
0
 
Harisha M GCommented:
It will be logged in your server. Why do you want another file for that ?
0
 
apleloisAuthor Commented:
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");?>
0
 
Harisha M GCommented:
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
}
0
 
apleloisAuthor Commented:
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);
?>
0
 
apleloisAuthor Commented:
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
0
 
Harisha M GCommented:
 fwrite ($IPsFile, $myip);
0
 
apleloisAuthor Commented:
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
0
 
Harisha M GCommented:
Try this:

if($_SERVER['REMOTE_ADDR'] != "1.2.3.4")
{
  $counter++;
  $myfile = fopen("ips.txt", "a");
  fwrite ($myfile, "$myip\n");
  fclose ($myfile);
}
0
 
apleloisAuthor Commented:
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);
?>
0
 
Harisha M GCommented:
Shouldn't that be $Count++ instead of $counter++ ?
0
 
apleloisAuthor Commented:
yes, thats what I was looking for. thank you very much sir!
0
 
Harisha M GCommented:
Glad I could help !

Thanks for the grade :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 13
  • 13
Tackle projects and never again get stuck behind a technical roadblock.
Join Now