Custom PHP log files

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to php basics my name is sean and today i want to show you how to create a simple customized log file for your website this is beneficial so you can track user activity if someone tries to access a page that they shouldn't be accessing or someone enters the incorrect username and password so many times or if they access data that they shouldn't be accessing or trying to hack around that sort of stuff you have a nice little log entry where you can view and see that sort of stuff just for demonstration i'm going to create a simple form where user has to guess a number and if the number is incorrect then it's going to write to that log file just so we can see how the thing works okay so i have an includes.p file and i also have an index.php file let's go ahead and start this form the method will equal post i'll just have a simple input field type will equal text and the name will just simply be number then i'll have a submit button and we can see that on the screen here okay so basically i can say if server request method is equal and the same byte type as post then number is going to equal the post value of number and i can say if number is not equal to 5 then echo number is incorrect else we can say echo number is correct okay so as it is right now if i enter one it's going to say one is incorrect but if i enter 5 it's going to say 5 is correct all right so now i want it to write to a log file if this number is incorrect i'm going to do all of that inside of this includes.php all right so we haven't really talked about functions a whole lot on this channel yet so you can also consider this kind of an introductory to how functions work i'm just going to create a new function and i'll call logger so we're going to send one argument to this and this is simply going to be log this is the contents of what we want to send to our file now when working with files a file may exist or it may not already exist so we need to check to see if that file exists or not and that's simple we just say if not file exists and we'll just call this file log.txt then file put contents to log.txt and we'll just send nothing to it so basically if the file doesn't exist we're going to create it but it's not going to have any content inside of it okay so what kind of information do i want to capture well let's say if i'm working on a live website a production website i want to capture the ip address i want to capture the date and time that the user did what they did and then have some sort of a custom text attribute to that user tried to access invalid page or entered incorrect credentials or something like that for the ip address that's simple that's just the server super global of remote addr we'll just call that client ip the time will equal a formatted date value of the month day and year hour and minutes we'll do am and then just specify that on the unix timestamp and then our log what we want to show up in the log file itself will be passed through as an argument so we won't specify that here inside the function so now let's get the contents of our existing log file so i'll just create a variable called contents and that's going to be file get contents of log dot txt all right so this is just grabbing the existing contents now we want to append data to that so i'll just create the same variable contents dot equals because we're appending data to it and then this is where we specify what we want to send to that file so let's just say the ip address and then we'll send a tab the time and then the log whatever that may be at the time let's go ahead and tab that out too and then we'll do a carriage return okay so it's going to show the ip address tab time tab and then the message that's displayed and that's it so now all we have to do is send that back to the file we can do that with file put contents we have to specify the file name which is log.txt and then contents or whatever we're adding to that this is all for this so let me go back to my index page and i want to include my includes.php file and here where it shows the number is incorrect all i'm going to do is say logger and then log and instead of typing what i want to inside of log i'm just going to create a variable so we'll just say log equals user entered incorrect number and then we'll just say the number inside of that okay so now whatever we have here is going to be passed through here so providing i don't have any syntax errors if i refresh my page and i hit the number 5 it's going to say 5 is correct if i do the number 1 it's going to say 1 is incorrect but now if i look back i've got a new file log.txt let's go ahead and open that up so i can see here my ip address the date and time which is totally incorrect and then the message that i'm actually logging let's go ahead and fix this date time here real quick and i can do that by jumping up above where i'm trying to get the time i'll just use a function called date default time zone set america chicago because i'm in central time and now if i enter eight or something like that look back at my log now it's got the correct time 904 pm so this is one way of doing this now one very big security issue with this is the fact that you're sending your log to the exact same file as your other files so if someone did like a wget request against your website they would be able to retrieve this text document we actually should probably store this outside of our htdocs folder so if i go back one two directories i can save this in my root x amp so to do that all we have to do is come in and change log.txt to account for those directories so we're going to do period period forward slash period period forward slash log.txt replace all of those so now whenever i refresh this if i look in my xampp folder i now have a log.txt that lives there instead so that wraps up this video if you have any questions or comments please leave those below and i appreciate you watching i'll see you on the next one
Info
Channel: phpBasics
Views: 9,330
Rating: undefined out of 5
Keywords: php, php tutorial, php logs, php function, php function tutorial
Id: _Q68prToQV8
Channel Id: undefined
Length: 6min 55sec (415 seconds)
Published: Thu Apr 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.