I need to log the visitors IP to a txtfile...<

Or, it doesn't have to be to a text file, but, just to someplace on the server, this sounds simple, not sure if it is or not, but surely someone already has a script written for this...or I hope :(why? not everybody shows there IP.

<!-- m --><a class="postlink" href="http://www.snippetlibrary.com/viewhtml.php?id=6&kid=14&siteid=234heres">http://www.snippetlibrary.com/viewhtml. ... d=234heres</a><!-- m --> something i whipped up quickly

<?php

// The location of where the log will be written too, remember to chmod to 777
$log_file = 'log.txt';

/* Should produce something like:
4:12 08-12-2003 - 12.23.45.67 View page test.php
*/
$insert = date("g:ia d-m-y") ." - " . $_SERVER['REMOTE_ADDR'] . " Viewed page " . $_SERVER['PHP_SELF'];

// Open the log file for apend
$fp = fopen($log_file,"a");

// Write $insert into the log file
fwrite($fp, $insert."\r\n\n");

// Close the file
fclose($fp);

?>

or if you want to keep logs for more then one page you could include it or something then call it with a function liks so:

<?php
function add_log($file)
{

// The location of where the log will be written too, remember to chmod to 777
$log_file = 'log.txt';

/* Should produce something like:
4:12 08-12-2003 - 12.23.45.67 View page test.php
*/
$insert = date("g:ia d-m-y") ." - " . $_SERVER['REMOTE_ADDR'] . " Viewed page ". $file;

// Open the log file for apend
$fp = fopen($log_file,"a");

// Write $insert into the log file
fwrite($fp, $insert."\r\n\n");

// Close the file
fclose($fp);

}
?>

then call the function with add_log($_SERVER['PHP_SELF']); (i'm sure that will work) :)

JacobWell that script looks good from what I can see, digitalmagik, but there might not be any need for it, since what you're basically doing here, is writing your own log of requests to the web server. Maybe the web server's log is available to you, bow-viper1? Many hosts share at least the access.log(apache) with their customers, and it would show more information as well.
Just a thought :)Thanks guys.

And I'll look into that, this is actually something a client requested from me, so its not actually for me. So I would have to check, anyhow, thanks again.i know rydberg ;), but that code was actually a modyfied version of part of a script i made that makes a log of uploaded filesHere is my index file, the file when visited, want the IP to be logged. I zipped it because you can't attach PHP files. log.txt is in the same folder as the index, CHMODDED to 777(log.txt is)..Just don't see whats wrong.Reuploaded, I think i uploaded the wrong one somehow..but here is the real thing.Not sure what I did, unless it takes like 30 mins to process then put it in the log file..but, its showing them now..so thanks, but just incase, check my file please, make sure it looks right.looks fine to me i guess. why are you using "\r\n\n" to end each line?Mmmm...not seeing it man, I only see that show up in the code at all twice.It appears once...

fwrite($fp, $insert."\r\n\n");

One "\n" too much, the line breaks will appear a bit strange. In Windows for example, you get three(!) 'square' characters at the end of the line. I don't know what it looks like on UNIX. "\r\n" is enough.woops :DThis is the code I use for page views: and you can use it as a really cheap counter
<?php

$fp = fopen("count.txt","r");
$count = fread ($fp, filesize ("count.txt"));
fclose($fp);
$fp = fopen("count.txt","w");
fwrite($fp, $count);
fclose($fp);
echo "Hits : $count";
?>

I use that code just for page views and not as a counter


Here is the code for a logger, it stores in a html file though, but it is nice.

<?php $visitor_time = date("F jS Y, h:iA");

if ($REMOTE_HOST == "") $visitor_info = "ACK No host";
else $visitor_info = $REMOTE_HOST;

if ($REMOTE_ADDR == "") $visitor_info2 = "ACK No address";
else $visitor_info2 = $REMOTE_ADDR;

if ($HTTP_REFERER == "") $visitor_info3 = "OH DEAR No referer";
else $visitor_info3 = $HTTP_REFERER;

if ($HTTP_USER_AGENT == "") $visitor_info4 = "WEIRDNESS No browser or other browser used";
else $visitor_info4 = $HTTP_USER_AGENT;

$fp = fopen("../adh/visitors.html", "a");
fputs($fp, "<br>?******* $visitor_time<br>$visitor_info<br>$visitor_info2<br>$visitor_info3<br>$visitor_info4<br>\n");
fclose($fp);
?>

no I didnt make those
 
Back
Top