[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

counter

Posted on 2006-05-20
26
Medium Priority
?
302 Views
Last Modified: 2008-03-06
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
Comment
Question by:aplelois
  • 13
  • 13
26 Comments
 
LVL 37

Accepted Solution

by:
Harisha M G earned 2000 total points
ID: 16726924
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
 

Author Comment

by:aplelois
ID: 16726937
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16726946
Did you change your IP there ?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:aplelois
ID: 16726948
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16726955
Yes it does that only, and for that, it should know YOUR IP, since it should count others.
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16726965
Just change 127.0.0.1 to your IP.

You can see your actual IP using "ipconfig" command (if you are using Windows)
0
 

Author Comment

by:aplelois
ID: 16726967
yup, I got my IP this ways

<?
$ipaddr = $HTTP_SERVER_VARS["REMOTE_ADDR"];
echo $ipaddr
?>
0
 

Author Comment

by:aplelois
ID: 16726970
so any idea what might be the problem ?
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16726974
Are you having static ip or dynamic ip ?
0
 

Author Comment

by:aplelois
ID: 16726980
its been static for the last 3yrs
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16726981
You need to put your IP inside that code. Do NOT use $HTTP_SERVER_VARS["REMOTE_ADDR"];
0
 

Author Comment

by:aplelois
ID: 16726991
ok now its working.... I had to change this
if($_SERVER['REMOTE_ADDR'] ==
to
if($_SERVER['REMOTE_ADDR'] !=
0
 

Author Comment

by:aplelois
ID: 16726998
now how do I add a section to write the ips in a text file called ips.txt??
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16727002
Oh yeah.. I should have put it the other way :D
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16727004
It will be logged in your server. Why do you want another file for that ?
0
 

Author Comment

by:aplelois
ID: 16727015
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16727078
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
 

Author Comment

by:aplelois
ID: 16727142
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
 

Author Comment

by:aplelois
ID: 16727144
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16727163
 fwrite ($IPsFile, $myip);
0
 

Author Comment

by:aplelois
ID: 16727190
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16727198
Try this:

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

Author Comment

by:aplelois
ID: 16727269
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16727289
Shouldn't that be $Count++ instead of $counter++ ?
0
 

Author Comment

by:aplelois
ID: 16727320
yes, thats what I was looking for. thank you very much sir!
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16727391
Glad I could help !

Thanks for the grade :)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month18 days, 17 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question