joeysarsenal Posted November 2, 2007 Share Posted November 2, 2007 ey guys, just adding finishing touches to my website, was wondering if there is a way to write to a log, when a user enters the website. So it writes to log with ip-address and date. thanks in advanced Quote Link to comment https://forums.phpfreaks.com/topic/75727-ip-addresss/ Share on other sites More sharing options...
pocobueno1388 Posted November 2, 2007 Share Posted November 2, 2007 Yes, there is a way. Your going to have to know how to get their IP address and the date $ip = $_SERVER['REMOTE_ADDR']; $date = date("Y-m-d"); Now your going to have to know how to write it to a file. http://www.tizag.com/phpT/filewrite.php Quote Link to comment https://forums.phpfreaks.com/topic/75727-ip-addresss/#findComment-383231 Share on other sites More sharing options...
joeysarsenal Posted November 2, 2007 Author Share Posted November 2, 2007 Thanks for the quick reply, ill have a play around with it now and let you know how go. Quote Link to comment https://forums.phpfreaks.com/topic/75727-ip-addresss/#findComment-383232 Share on other sites More sharing options...
joeysarsenal Posted November 2, 2007 Author Share Posted November 2, 2007 Yes, there is a way. Your going to have to know how to get their IP address and the date $ip = $_SERVER['REMOTE_ADDR']; $date = date("Y-m-d"); Now your going to have to know how to write it to a file. http://www.tizag.com/phpT/filewrite.php thats the example they had at that site. With other information to open and overwrite. But i think this one is more revelant " $myFile = "testFile.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "Bobby Bopper\n"; fwrite($fh, $stringData); $stringData = "Tracy Tanner\n"; fwrite($fh, $stringData); fclose($fh);" <?php $ip = $_SERVER['REMOTE_ADDR'];//variable ? $date = date("Y-m-d"); //variable ? $myFile = "log.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = $ip"; fwrite($fh, $stringData); $stringData = "$date"; fwrite($fh, $stringData); fclose($fh);" ?> cant really test it here. No Xampp on these comps. Quote Link to comment https://forums.phpfreaks.com/topic/75727-ip-addresss/#findComment-383236 Share on other sites More sharing options...
pocobueno1388 Posted November 2, 2007 Share Posted November 2, 2007 You have a few errors <?php $ip = $_SERVER['REMOTE_ADDR'];//variable ? $date = date("Y-m-d"); //variable ? $myFile = "log.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = $ip; fwrite($fh, $stringData); $stringData = $date; fwrite($fh, $stringData); fclose($fh); ?> Quote Link to comment https://forums.phpfreaks.com/topic/75727-ip-addresss/#findComment-383239 Share on other sites More sharing options...
joeysarsenal Posted November 2, 2007 Author Share Posted November 2, 2007 ty i let you know how i go at home. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/75727-ip-addresss/#findComment-383240 Share on other sites More sharing options...
cooldude832 Posted November 2, 2007 Share Posted November 2, 2007 before you do this, that will fail because you have the 'w type of opening which will wipe the file clean on each rewrite, use the 'a' instead of w and you be better. also you have no delimiter in there, and you can write it all in 1 srting try <?php $ip = $_SERVER['REMOTE_ADDR'];//variable ? $date = date("Y-m-d"); //variable ? $myFile = "log.txt"; $fh = fopen($myFile, 'a') or die("can't open file"); $stringData = $date.", ".$ip."\r\r\n"; fwrite($fh, $stringData); fclose($fh); ?> that has a delimiter and it spaces it like Date, IP Date, IP for ease of reading. Quote Link to comment https://forums.phpfreaks.com/topic/75727-ip-addresss/#findComment-383243 Share on other sites More sharing options...
joeysarsenal Posted November 2, 2007 Author Share Posted November 2, 2007 yer i didnt think that "delimiter" was imported so i took it out it was the first time ive ever used something like this. Thanks for all the help guys. Quote Link to comment https://forums.phpfreaks.com/topic/75727-ip-addresss/#findComment-383247 Share on other sites More sharing options...
cooldude832 Posted November 2, 2007 Share Posted November 2, 2007 technically its not, but if you want to read the file without a bunch of work it is. Quote Link to comment https://forums.phpfreaks.com/topic/75727-ip-addresss/#findComment-383249 Share on other sites More sharing options...
Zane Posted November 2, 2007 Share Posted November 2, 2007 Webservers OSs usually create an access log with this information already in them Quote Link to comment https://forums.phpfreaks.com/topic/75727-ip-addresss/#findComment-383268 Share on other sites More sharing options...
atlanta Posted November 2, 2007 Share Posted November 2, 2007 yer i didnt think that "delimiter" was imported so i took it out it was the first time ive ever used something like this. Thanks for all the help guys. I say the delimiter is important it will allow you to read the access log better. Quote Link to comment https://forums.phpfreaks.com/topic/75727-ip-addresss/#findComment-383272 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.