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($Te xtFile));
$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);
?>
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($Te
$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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Did you change your IP there ?
ASKER
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!
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)
You can see your actual IP using "ipconfig" command (if you are using Windows)
ASKER
yup, I got my IP this ways
<?
$ipaddr = $HTTP_SERVER_VARS["REMOTE_ ADDR"];
echo $ipaddr
?>
<?
$ipaddr = $HTTP_SERVER_VARS["REMOTE_
echo $ipaddr
?>
ASKER
so any idea what might be the problem ?
Are you having static ip or dynamic ip ?
ASKER
its been static for the last 3yrs
You need to put your IP inside that code. Do NOT use $HTTP_SERVER_VARS["REMOTE_ ADDR"];
ASKER
ok now its working.... I had to change this
if($_SERVER['REMOTE_ADDR'] ==
to
if($_SERVER['REMOTE_ADDR'] !=
if($_SERVER['REMOTE_ADDR']
to
if($_SERVER['REMOTE_ADDR']
ASKER
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 ?
ASKER
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");?>
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
}
if($_SERVER['REMOTE_ADDR']
{
$counter++;
// open file
// put the new ip
// close file
}
ASKER
well I coded this but is not working
<?
$HitsFile = "hits.txt";
$IPsFile = "ips.txt";
$Count = trim(file_get_contents($Hi tsFile));
$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);
?>
<?
$HitsFile = "hits.txt";
$IPsFile = "ips.txt";
$Count = trim(file_get_contents($Hi
$FP = fopen($HitsFile, "r");
$Count=fgets($FP, 4096);
fclose ($FP);
$myip = $_SERVER['REMOTE_ADDR'];
settype($Count, "integer");
if($_SERVER['REMOTE_ADDR']
{
$counter++;
fopen($IPsFile, "r");
fwrite ($myip);
fclose ($IPsFile);
}
if ($FP = fopen ($HitsFile, "w")){
fwrite ($FP, $Count);
fclose ($FP);
}
echo ($Count);
?>
ASKER
Warning: Wrong parameter count for fwrite() in /home/virtual/site13/fst/v ar/www/htm l/counter/ counter.ph p on line 19
Warning: fclose(): supplied argument is not a valid stream resource in /home/virtual/site13/fst/v ar/www/htm l/counter/ counter.ph p on line 20
Warning: fclose(): supplied argument is not a valid stream resource in /home/virtual/site13/fst/v
fwrite ($IPsFile, $myip);
ASKER
Warning: fwrite(): supplied argument is not a valid stream resource in /home/virtual/site13/fst/v ar/www/htm l/counter/ counter.ph p on line 18
Warning: fclose(): supplied argument is not a valid stream resource in /home/virtual/site13/fst/v ar/www/htm l/counter/ counter.ph p on line 19
Warning: fclose(): supplied argument is not a valid stream resource in /home/virtual/site13/fst/v
Try this:
if($_SERVER['REMOTE_ADDR'] != "1.2.3.4")
{
$counter++;
$myfile = fopen("ips.txt", "a");
fwrite ($myfile, "$myip\n");
fclose ($myfile);
}
if($_SERVER['REMOTE_ADDR']
{
$counter++;
$myfile = fopen("ips.txt", "a");
fwrite ($myfile, "$myip\n");
fclose ($myfile);
}
ASKER
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($Hi tsFile));
$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);
?>
<?
$HitsFile = "hits.txt";
$Count = trim(file_get_contents($Hi
$FP = fopen($HitsFile, "r");
$Count=fgets($FP, 4096);
fclose ($FP);
$myip = $_SERVER['REMOTE_ADDR'];
settype($Count, "integer");
if($_SERVER['REMOTE_ADDR']
{
$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++ ?
ASKER
yes, thats what I was looking for. thank you very much sir!
Glad I could help !
Thanks for the grade :)
Thanks for the grade :)
ASKER
<?
$TextFile = "hits.txt";
$Count = trim(file_get_contents($Te
$FP = fopen($TextFile, "r");
$Count=fgets($FP, 4096);
fclose ($FP);
settype($Count, "integer");
if($_SERVER['REMOTE_ADDR']
$Count++;
if ($FP = fopen ($TextFile, "w")){
fwrite ($FP, $Count);
fclose ($FP);
}
echo($Count);
?>