Script to append Server side variable to text file on server
I want to be able to write the address of the remote user <!--#echo var="remote_addr" --> to a text file on the server automatically when either the user clicks a button or when they visit a certain web page.
I am frankly stumped. Does anyone know of a php script that will do this?
Or any other way to do this.
Any help is greatly appreciated.
I have this so far, but I am unable to get new information to add itself to a new line and can't get both variables to work.
<?php
$filename = 'test.txt';
$ip = $_SERVER['REMOTE_ADDR'] . "\n";
$name = $_SERVER['REMOTE_USER'] . "\n";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $ip, $name) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($ip) and ($name) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>
Last edited by robp2175; 02-05-2009 at 12:15 PM.
|