Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing to text file if not a certain IP

Status
Not open for further replies.

rossmcl

Programmer
Apr 18, 2000
128
Hi
I am very new to PHP, so this is probably me being stupid.
When someone visits my website, I write to a file the date, time, and IP address of visitor. Works fine, but the annoying thing is whilst I am testing the site, I get my IP address coming up on the site a lot.
I want to put in a temporary IF statement into my code until I finish doing work on it, so that if the REMOTE_ADDR of visitor is me, it doesnt write to the file.

This is the code I am using, but it doesnt seem to work. When I put it this code in, it doesnt write anything, even when people that are not the 212.... IP address visit the site. I suspect I am treating the IP address as a string or something, but I have tried double quotes and single quotes around the IP address, and neither work.

Can someone please help me. Would very much appreciate it.

RM




<?php

$filename = &quot;VisitorLog.txt&quot;;
$handle = fopen ($filename, &quot;a+&quot;);
$todaydate = date(&quot;Y-m-d H:i:s&quot;);
$visitorInfo = $HTTP_SERVER_VARS['REMOTE_ADDR'];

If ($visitorInfo) = &quot;212.12.12.12&quot;

{

}
else
{
fwrite($handle, &quot;\r\n&quot; . $todaydate . ' ' . $visitorInfo);
}

fclose( $handle );

// \r\n
?>
 
You are only using a single equals sign (=) in the if statement. Using double equals (==)
Code:
<?php

$filename = &quot;VisitorLog.txt&quot;;
$handle = fopen ($filename, &quot;a+&quot;);
$todaydate = date(&quot;Y-m-d H:i:s&quot;);
$visitorInfo = $HTTP_SERVER_VARS['REMOTE_ADDR'];

if ($visitorInfo == &quot;212.12.12.12&quot;)
{
// do nothing
}
else
{
fwrite($handle, &quot;\r\n&quot; . $todaydate . '  ' . $visitorInfo); 
}
fclose( $handle ); 
// \r\n 
?>
 
A couple of things about this :
$http_SERVER_VARS['REMOTE_ADDR']; will fail (typo on http which should be HTTP)

$HTTP_SERVER_VARS[] is I believe depreciated and should instead be replaced by $_SERVER[]

Why do nothing if the value is tru, why not check that it is in fact false instead.

Code:
<?php

$filename = &quot;VisitorLog.txt&quot;;
$handle = fopen ($filename, &quot;a+&quot;);
$todaydate = date(&quot;Y-m-d H:i:s&quot;);
$visitorInfo = $_SERVER['REMOTE_ADDR'];

if ($visitorInfo != &quot;212.12.12.12&quot;){

	 fwrite($handle, &quot;$todaydate \t $visitorInfo \r\n&quot;);

}

fclose($handle);

?>

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
KarveR
Just wanted to say thanks. I spent an hour today wondering why my code had stopped working, till I realised it was the lowercase HTTP.

And thanks for your alternative.

Very much appreciated.
RM
 
Westbury:
Actually, it may not be your fault. There is a problem with the TGML system in that, even if inside a
Code:
...
or [ignore]...[/ignore] block, always converts &quot;HTTP&quot; to &quot;http&quot;.

I have reported the problem to TT management. They had a complete rewrite of the TGML system planned for next month anyway.

The only way to keep &quot;HTTP&quot; in uppercase is to turn off all TGML processing for a post.


All:
The manual states that the preferred method of accessing external variables is through use of the superglobal arrays ($_GET, $_POST, $_SERVER). The longer global &quot;$HTTP_*_VARS&quot; arrays are deprecated -- PHP 5.0.0 even has a feature to turn off the $HTTP_*_VARS arrays off completely.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top