0

Okay so basically the script works, but every time i refresh the data.txt file is over-written, i want it to add each individual entry into the data.txt in a new file.

example: 1.1.1.1 July 18th @ 2:03:17 pm

<?php
date_default_timezone_set("Europe/London");
function GetAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

$adresseip = GetAddr();

function sec2hms($sec, $padHours = false) {
        @$hms = "";
        @$days = intval($sec/86400);
        if($days > 0 ) {
            if($days == 1) {
                @$hms .= (($padHours)?str_pad($hours, 2, "0", STR_PAD_LEFT).':':@$days.' Day');
            } else {
                @$hms .= (($padHours)?str_pad($hours, 2, "0", STR_PAD_LEFT).':':@$days.' Days');
            }
        }
        @$sec-= ($days*86400);
        @$hours = intval(intval($sec) / 3600);
        if($hours > 0) {
            if($days > 0) { @$s = ', '; }
            if($hours == 1) {
                @$hms .= @$s.(($padHours)?str_pad($hours, 2, "0", STR_PAD_LEFT).':':@$hours.' Hour');
            } else {
                @$hms .= @$s.(($padHours)?str_pad($hours, 2, "0", STR_PAD_LEFT).':':@$hours.' Hours');
            }
        }
        @$minutes = intval(($sec / 60) % 60);
        if($minutes > 0) {
            if($hours > 0) { @$d = ', '; }
            if($minutes == 1) {
                @$hms .= @$d.str_pad($minutes, 2, "0", STR_PAD_LEFT) . ' Minute';
            } else {
                @$hms .= @$d.str_pad($minutes, 2, "0", STR_PAD_LEFT) . ' Minutes';
            }
        }
        @$seconds = intval($sec % 60);
        if($seconds > 0) {
            if($minutes > 0) { @$p = ', '; }
            if($seconds == 1) {
                @$hms .= @$p.str_pad($seconds, 2, "0", STR_PAD_LEFT) . ' Second';
            } else {
                @$hms .= @$p.str_pad($seconds, 2, "0", STR_PAD_LEFT) . ' Seconds';
            }
        }

        return @$hms;
    }
function report($data) {
        $time = date('g:i:s A', time());
        echo "[$time] $data\n";
    }
$d= date('F jS @ g:i:s a'); 
$fp = fopen('data.txt', 'r+');
fwrite($fp,"$adresseip - $d");
fclose($fp);
?>
  • You have `$fp = fopen('data.txt', 'r+');` Try either `$fp = fopen('data.txt', 'a+');` or `$fp = fopen('data.txt', 'a');`. Both will work, the difference is one will add to the top of the file, while the other will add to the last entry. Take your pick. – Funk Forty Niner Jul 18 '13 at 13:10

2 Answers2

0

This:

$fp = fopen('data.txt', 'r+');

puts the pointer at the beginning of the file, causing an overwrite. Use

$fp = fopen('data.txt', 'a+');

instead.

More in the documentation: http://php.net/manual/en/function.fopen.php:

'r+' Open for reading and writing; place the file pointer at the beginning of the file.

'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

Community
  • 1
  • 1
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
0

You need to use fopen in append mode:

$fp = fopen('data.txt', 'a+');

When you use fopen with mode r+, the file pointer is at the beginning of the file, which causes you to overwrite your data.

jh314
  • 27,144
  • 16
  • 62
  • 82