1

I have a script that I am working on in PHP that logs IP Address's of visters that go to that specific page. This is just a prototype. The script that I have concocted is working, but when it creates the TXT file containing the IP's it only does one line. How can I make it keep adding a line for every visitor regardless if they are a repeat visitor. I am not really sure how to go about that part, I am new to PHP.

Here is what I have so far:

<?PHP $ip = getenv("REMOTE_ADDR"); $date = date("d") . " " . date("F") . " " . date("Y"); $intofile = $ip . "n" . $date; $hfile = fopen("ip-address.txt", "w"); fwrite($hfile, $intofile); fclose($hfile); ?> <!DOCTYPE html> <html language="en-us"> <head> <title>IP Address Logging Software</title> <link rel="stylesheet" type="text/css" href="Source/Stylesheet/DefaultPage.css" /> <link rel="stylesheet" type="text/css" href="Source/Stylesheet/DefaultPage.css" /> <link rel="stylesheet" type="text/css" href="Source/Stylesheet/DefaultPage.css" /> <script type="text/javascript" src="Source/Javascript/DefaultScript.css"></script> <script type="text/javascript" src="Source/Javascript/DefaultScript.css"></script> <script type="text/javascript" src="Source/Javascript/DefaultScript.css"></script> </head> <body language="en-us"> <?PHP $ip=$_SERVER['REMOTE_ADDR']; echo "<strong>Your IP Address <em>$ip</em> Has Been Logged</strong>"; ?> </body> </html> 
6
  • 2
    why not use the apache access.log and filter by url. much faster and no extra implementation necessary. don't use getenv this is deprecated use $_SERVER Commented Jul 17, 2014 at 20:28
  • flat files dont scale for storage, why not use a db Commented Jul 17, 2014 at 20:31
  • @dagon: because apache writing to a flat-file access_log is a lot "cheaper" computationally than trying to stuff into a database. Commented Jul 17, 2014 at 20:32
  • As an FYI, write the log outside of the public directory so users can't access it. Commented Jul 17, 2014 at 20:33
  • @MarcB i voted that up, my first pick would be to use the Apache access log, 2nd a db Commented Jul 17, 2014 at 20:33

2 Answers 2

2
$hfile = fopen("ip-address.txt", "w"); 

Refer to the manual for fopen:

'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

Everytime you open the file it is being truncated. Use the 'a+' instead, or another one that will open the file and append, rather than delete what's already there.

Sign up to request clarification or add additional context in comments.

6 Comments

lot easier to just use file_put_contents('ip-address.txt', $data, FILE_APPEND)
So, if I replace the "w" with "a+" it will create another line in the txt file and log the next ip...? Instead of It rewriting the file every time and only keeping one ip at a time.
yes, I personally would follow @Marc B's advice, however.
dustin look at the fopen documentation
@DustinAngeletti: php.net/fopen just go read the docs. they CLEARLY explain what all the options do.
|
0

Alternatively use file_put_contents (http://php.net/manual/en/function.file-put-contents.php). Its basically a helper function that does the same as calling fopen(),fwrite(), and fclose().

And to make sure that it appends the content just add the FILE_APPEND flag:

file_put_contents('file.txt', 'Hello World', FILE_APPEND); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.