p3nguattack Posted August 15, 2012 Share Posted August 15, 2012 Hello everyone, I've been working on a function for a logfile for my site that logs when users login. I have it working sort of, but the problem I'm having is very annoying, and makes the logfile useless.. Anyway, basically what I have set up is a script that automatically makes a folder for each month, and a new logfile every day, and it (is supposed to) logs all the logins in the file that day then makes a new file the next day. My problem is after the initial creation and the first login log, it will no longer update. I haven't tried to make it delete and write a new line, because I just assumed that would work since the first line will work. It just won't append anything else to the file the next time a user logs in. The code should work, as far as I know it's coded correctly for what I want it to do. It's just not appending for whatever reason. I've made two functions, one using fopen/fwrite/fclose and the other uses file_put_contents($with, FILE_APPEND) and neither append as their supposed to. The data is just the same every time(it doesn't change at all). The permissions on the folders/files are 0666 (I'm using windows 7 and it won't let me chmod to 0777, but 0666 should work too) and since it creates/writes the first time, I don't see why it wouldn't be allowed to append to the file, so I doubt it's a permissions issue. Please take a look at my code and tell me what you think I should do to make this work the way I need it to. function log_action($action, $message="") { if(isset($action)) { $dt = time(); $datetime = strftime("%m-%d-%Y %I:%M:%S", $dt); $date = strftime("%m-%d-%Y", $dt); $dirname = strftime("%m-%Y", $dt); $logfile = SITE_ROOT.DS.'logs'.DS.$dirname.DS."logfile_".$date.".txt"; $data = strftime("%m-%d-%Y %I:%M:%S", $dt)." | Login: {$message} logged in.\r\n"; chdir(SITE_ROOT.DS.'logs'); if(!is_dir($dirname)) { mkdir(SITE_ROOT.DS.'logs'.DS.$dirname); //if($handle = fopen($logfile, "a")) { //$content = fwrite($handle, strftime("%m-%d-%Y %I:%M:%S", $dt)." | Login: {$message} logged in.\r\n"); //fclose($handle); //} else { //$message = "The log file could not be accessed."; //} if(file_put_contents($logfile, $data, FILE_APPEND)) { } else { echo "logfile could not be accessed"; } } return $message; } } I know it's pretty sloppy, but there's both codes with the fopen/fwrite/fclose portion commented out. That's the method I'd prefer to use, but I figured I'd show you both so you can see how it should go. I'm also aware there's more variables than necessary, but I was troubleshooting and made them so the code could be changed easier. I appreciate your replies in advance! Thanks. P.S. On my login page I have $action set to my login confirmation variable which is set when a user successfully logs in, and $message is set to their username, so it displays correctly in the output. I plan to make this a lot more versatile in the future, but I'd like to have it working first . Quote Link to comment https://forums.phpfreaks.com/topic/267092-cant-get-my-logfile-script-to-append-info-into-the-file-at-all/ Share on other sites More sharing options...
maxudaskin Posted August 15, 2012 Share Posted August 15, 2012 Change if(file_put_contents($logfile, $data, FILE_APPEND)) { } else { echo "logfile could not be accessed"; } to if(!file_put_contents($logfile, $data, FILE_APPEND)) { echo "logfile could not be accessed"; } Quote Link to comment https://forums.phpfreaks.com/topic/267092-cant-get-my-logfile-script-to-append-info-into-the-file-at-all/#findComment-1369503 Share on other sites More sharing options...
kicken Posted August 15, 2012 Share Posted August 15, 2012 <?php function log_action($action, $message="") { $dt = time(); $datetime = strftime("%m-%d-%Y %I:%M:%S", $dt); $date = strftime("%m-%d-%Y", $dt); $dirname = strftime("%m-%Y", $dt); $logfile = SITE_ROOT.DS.'logs'.DS.$dirname.DS."logfile_".$date.".txt"; $data = strftime("%m-%d-%Y %I:%M:%S", $dt)." | Login: {$message} logged in.\r\n"; chdir(SITE_ROOT.DS.'logs'); if(!is_dir($dirname)) { mkdir(SITE_ROOT.DS.'logs'.DS.$dirname); if(file_put_contents($logfile, $data, FILE_APPEND)) { } else { echo "logfile could not be accessed"; } } return $message; } So after a little re-formatting so it is readable, do you see what the problem is now? Your only logging something if the file doesn't exist. Once you've created it, your function does nothing. Quote Link to comment https://forums.phpfreaks.com/topic/267092-cant-get-my-logfile-script-to-append-info-into-the-file-at-all/#findComment-1369504 Share on other sites More sharing options...
p3nguattack Posted August 15, 2012 Author Share Posted August 15, 2012 Max, I think you missed the point xD, but thanks anyway! Kicken, I can't believe I overlooked that! It's always best to have another eye around for things like this. So, that brings me to wondering, if you think I should rewrite this function to make it work, or just separate the directory part altogether and make it another function? I was considering separating them before, but not for that reason, I hadn't even noticed that. I was just going to do it so I could use the directory function for other tasks. Thank you so much! Update: By the way, I just used moved the curly braces around a little (separated the directory portion from the fopen/ect. portion) and it worked just as it was supposed to, I'd still like your opinion though. I'm actually considering just making a log class to do it all. I think that might be an even better option, because I plan to do more with it. Quote Link to comment https://forums.phpfreaks.com/topic/267092-cant-get-my-logfile-script-to-append-info-into-the-file-at-all/#findComment-1369505 Share on other sites More sharing options...
kicken Posted August 15, 2012 Share Posted August 15, 2012 I would probably leave it together but it doesn't mater too much either way really. The only suggestion I give really is to not use chdir() since it might have a negative effect on other areas of your scripts. For instance if you had a script that say manipulated some images that are in the same dir as the script: <?php $im = imagecreatefromjpeg("somefile.jpg"); log_action("opened first file"); $im2 = imagecreatefromjpeg("someotherfile.jpg"); log_action("Opened second file"); The second imagecreatefromjpeg would fail because your log function changed the working directory to where your logs are rather than where the script is. Quote Link to comment https://forums.phpfreaks.com/topic/267092-cant-get-my-logfile-script-to-append-info-into-the-file-at-all/#findComment-1369508 Share on other sites More sharing options...
p3nguattack Posted August 15, 2012 Author Share Posted August 15, 2012 Hmm, that's a good point. I don't know why I even put it in there honestly, I think I was having a syntax error using mkdir() so I just made it move to the directory first instead of debugging it thanks for the suggestion, I really appreciate your input. I'll go ahead and take it out, and if I can't get it to auto create the dated folders in the right place without it, I'll just put another chdir() at the end of the function to have it move back. Quote Link to comment https://forums.phpfreaks.com/topic/267092-cant-get-my-logfile-script-to-append-info-into-the-file-at-all/#findComment-1369718 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.