Jump to content

Logging System


Frieling
 Share

Recommended Posts

Alright well,I am trying to become a developer over at a Anti cheat community. A admin told me to make a logging system.I got most of it but it still isnt logging correctly. So i need a fresh start. Here is how he would like it:He want it to be logged in a txt file. using arrays. Needs to be the persons IP, and there timestamp.needs a statement where if the log is older than 15 minutes, it gets deleted.Anything?

Link to comment
Share on other sites

What do you have so far? What do you need help with?
That would be handy.But as a starting point: logging this in a text file and using arrays. You might want to look at a function called file().It reads out each row as an array.dbase.txt:
row1 cell1 |&| row1 cell2 |&| row1 cell3row2 cell1 |&| row2 cell2 |&| row2 cell3row3 cell1 |&| row3 cell2 |&| row3 cell3

somephpfile.php:

//add code checking if file exists, to stop errors when trying to open...$data = file('dbase.txt');//$data holds this info//$data[0] = row1 cell1 |&| row1 cell2 |&| row1 cell3//$data[1] = row2 cell1 |&| row2 cell2 |&| row2 cell3//$data[2] = row3 cell1 |&| row3 cell2 |&| row3 cell3//use explode for each line.foreach($data as $key => $val){  $data[$key] = explode("|&|", $data);}

After this the array would correspond to your dbase.txt file like this:

 row1 cell1 |&| row1 cell2 |&| row1 cell3 row2 cell1 |&| row2 cell2 |&| row2 cell3 row3 cell1 |&| row3 cell2 |&| row3 cell3$data[0][0]|&|$data[0][1]|&|$data[0][2]$data[1][0]|&|$data[1][1]|&|$data[1][2]$data[2][0]|&|$data[2][1]|&|$data[2][2]

I used '&' as part of the separator because if you encode the input (if you plan on storing user input) then you can encode the ampersand so that your database never breaks.. I did use double pipes but found they broke if I put in a code example such as = if(x||y)Useful functions might be: htmlentities() - prevents javascript injections etc. trim() - remove unnecessary spaces. and use the following if you want to preserve layout of text by using html line break tag <br /> $linebr=array("\r\n", "\n", "\r"); //should work for all types of line breaks? $input = str_replace($linebr,"<br />", $input);$input being the users submitted data in a variable..

Edited by Reiyan
Link to comment
Share on other sites

By the OP's statement "Needs to be the persons IP, and there timestamp", there will be no user input saved, so the requirements will be even simpler - saving and then periodically retrieving two columns per record, IP and timestamp, with the retrieval file checking the timestamp and deleting records older than 15 minutes.So the question remains: what part of this do you need help with?

Edited by gjh42
Link to comment
Share on other sites

You can find the user's IP in $_SERVER['REMOTE_ADDR']. You can use the date function to format the timestamp however you want it.http://www.php.net/manual/en/function.date.phpTo write to a text file you would use fopen, fwrite, and fclose.http://www.php.net/manual/en/function.fwrite.php

Link to comment
Share on other sites

  • 3 weeks later...

okay,its logging it correctly now. The last problem is that it is going in a row, instead of on the next line each time.this is what i have:

$log_file = "log.txt";$date = dateĀ®;$ip = $_SERVER['REMOTE_ADDR'];	$handle = fopen($log_file , "a");$write = "$ip | $date<br />";fwrite($handle , $write);fclose($handle);echo "Your ip has been logged!";

Edited by frieling
Link to comment
Share on other sites

What jlhaslip showed will print a line of text with a linebreak at the end of it, which will make sure that each line ends in a linebreak so that the next line starts on a new line. Using <br> in a text file isn't a line break, it's 4 characters. Text files aren't HTML files, HTML formatting doesn't work in plain text files. That's what the linebreak is for. If you only ran the code once and checked it and saw that it showed up on the same line, keep in mind the linebreak is on the end of the line, not the beginning. If you run it again you'll see the next line show up on its own line.

Link to comment
Share on other sites

Right, so you write this to the file:$write = "$ip | $date\r\n";That writes the ip and date, then \r writes a carriage return character and \n writes a newline character, Windows uses \r\n to mean a new line in a text file. Linux just uses \n. So that writes the data then your newline character. The next time you write data it will be on the next line, because the last character you wrote was a newline.

Link to comment
Share on other sites

You can use the file function to read a file into an array, you can use array_shift to remove the first item from an array (keep shifting off the first element until it's new enough), then use implode to join the array back into a series of lines and use file_put_contents to write it back to the file. You can find the references for each of those functions in the manual at php.net.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...