Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #15138

Re: dans un log.txt : reduire IP à deux octets selon recomm CNIL

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.php
Subject Re: dans un log.txt : reduire IP à deux octets selon recomm CNIL
Date 2015-03-28 22:47 +0100
Organization PointedEars Software (PES)
Message-ID <13863983.OW5F8XF2sQ@PointedEars.de> (permalink)
References <5516e98a$0$2988$426a34cc@news.free.fr> <9RCRw.180861$jx4.20363@fx27.iad>

Show all headers | View raw


Lew Pitcher wrote:

> […] "bird" […] wrote:
>> //fwrite($fp, "$time $ip $page $browser");
>   fwrite($fp, "$time $ip[0].$ip[1] $page $browser");

The log file created by this code will be unreadable and hardly parseable 
because it will have only one long line: the trailing newline of each entry 
is missing – fwrite() does not write it by default.

As for the rest, I recommend to use braces, concatenation, or (v)sprintf() 
to avoid ambiguities –

  fwrite($fp, "{$time} {$ip[0]}.{$ip[1]} {$page} {$browser}\n");

or

  fwrite($fp, $time . ' ' . $ip[0] . '.' . $ip[1] . ' ' . $page
    . ' ' . $browser . "\n");

or

  fwrite($fp,
    vsprintf("{$time} %s.%s {$page} {$browser}\n",
      array_slice(explode('.', $_SERVER['REMOTE_ADDR']), 0, 2)));

or

  fwrite($fp,
    sprintf("%s %s.%s %s %s\n", $time, $ip[0], $ip[1], $page, $browser));

(in this simple case I would probably use braces, if only for “$ip[0]” and 
“$ip[1]” to make clear that there is no concatenation.  But note that Web 
servers already can write such log files in a non-blocking fashion; there is 
hardly a need to reinvent the wheel in a worse way) – …

>> fclose($fp);
>> //-------------------------------------------------------------
>> 
>> comment réduire l'ip nnnn.mmm.oooo.pppp à nnnn.mmm ?
>> 
>> merci

… and, although and because this is an international newsgroup, to post here 
in English only (in order to reach the widest possible audience), and to 
post in French to <news:fr.comp.lang.php> instead.

-- 
PointedEars
Zend Certified PHP Engineer
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

dans un log.txt : reduire IP à deux octets selon recomm CNIL bird <new.bird@free.fr> - 2015-03-28 18:48 +0100
  Re: dans un log.txt : reduire IP à deux octets selon recomm CNIL Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-03-28 14:58 -0400
    Re: dans un log.txt : reduire IP à deux octets selon recomm CNIL Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-28 22:47 +0100
      in a  log.txt : how convert IP.IP.IP.IP to IP.IP.*.* (hope of french CNIL) ? bird <new.bird@free.fr> - 2015-03-31 10:52 +0200
        Re: in a  log.txt : how convert IP.IP.IP.IP to IP.IP.*.* (hope of french CNIL) ? Jerry Stuckle <jstucklex@attglobal.net> - 2015-03-31 09:16 -0400
        Re: in a  log.txt : how convert IP.IP.IP.IP to IP.IP.*.* (hope of french CNIL) ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-31 22:28 +0200
  Re: dans un log.txt : reduire IP à deux octets selon recomm CNIL Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2015-03-28 20:15 +0100
    Re: dans un log.txt : reduire IP à deux octets selon recomm CNIL Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-28 22:50 +0100

csiph-web