$_sever['remote_addr']

software development

#1

I am trying to get the IP address of users who access a certain page, but the script is returning errors. My script is something like this…

$logfile = fopen("log.txt", "a"); fwrite($logfile, $_SERVER['REMOTE_ADDR']); fclose($logfile); I was browsing the Dream Host wiki and looking into the php.ini settings and found this page: http://wiki.dreamhost.com/index.php/Register_globals
According to that we should be able to use $_SERVER, right? So what have I done wrong? Or, if anybody has an alternate script to try, that would be just as good as fixing mine.

Thanks.


#2

Does log.txt file exist? If I remember right the append option requires the file to already exist. The way you specified it the log.txt file will be in the same directory as your script.
$filename=‘log.txt’;
$io=‘w’;
$fh = fopen($filename, $io) or die($php_errormsg);
$text= $_SERVER[‘REMOTE_ADDR’];
fwrite($fh,$text);
fclose($fh) or die($php_errormsg);
Silk

My website


#3

Thanks for the reply.

The file does already exist. My reason for appending is that I am trying to keep a log of users who update their profiles on my site.

However, I changed my script to match this…

…it worked just fine.

So I guess there was some problem writting it to the file without storing it in a variable or something along those lines.


#4

Glad you got it working.
Silk

My website


#5

Sometimes, adding braces does the trick. I didn’t test this, but something like this might work:

fwrite($logfile, [color=#CC0000]{[/color]$_SERVER[‘REMOTE_ADDR’][color=#CC0000]}[/color]);

The way you fixed it is also fine–just something to keep in mind in case it comes in handy sometime.


:stuck_out_tongue: Save up to $96 at Dreamhost with ALMOST97 promo code (I get $1).
Or save $97 with THEFULL97.