Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

8 views
Skip to first unread message

bird

unread,
Mar 28, 2015, 1:49:05 PM3/28/15
to
bonjour,
mon log (base moteur lionwiki) est créé par les lignes:

// -------------------------------------------------------------
// idem (selon méthode Chuwiki 3, page ip_log historique version 2006-06-06)
$time = date("Y-m-d H:i"); // repris de 2ème méthode ip_log de chuwiki
$ip = $_SERVER["REMOTE_ADDR"];
$page = $_SERVER['REQUEST_URI'];
$browser = $_SERVER['HTTP_USER_AGENT'];
$fp = fopen("./var/log/log.txt", "a");
fwrite($fp, "$time $ip $page $browser
");
fclose($fp);
//-------------------------------------------------------------

comment réduire l'ip nnnn.mmm.oooo.pppp à nnnn.mmm ?

merci

Lew Pitcher

unread,
Mar 28, 2015, 2:58:20 PM3/28/15
to
On Saturday March 28 2015 13:48, in comp.lang.php, "bird" <new....@free.fr>
wrote:

> bonjour,
> mon log (base moteur lionwiki) est créé par les lignes:
>
> // -------------------------------------------------------------
> // idem (selon méthode Chuwiki 3, page ip_log historique version
> 2006-06-06) $time = date("Y-m-d H:i");

Une façon de faire serait ...


> // $ip = $_SERVER["REMOTE_ADDR"];
$ip = explode(".",$_SERVER["REMOTE_ADDR"]);

> $page = $_SERVER['REQUEST_URI'];
> $browser = $_SERVER['HTTP_USER_AGENT'];
> $fp = fopen("./var/log/log.txt", "a");

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

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


--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request

Thomas Mlynarczyk

unread,
Mar 28, 2015, 3:15:33 PM3/28/15
to
bird schrieb:
> comment réduire l'ip nnnn.mmm.oooo.pppp à nnnn.mmm ?

// par exemple
$ip = '10.192.0.34';
var_dump( preg_replace( '/(\.\d+){2}$/', '', $ip ) ); // '10.192'

// ou bien
$ip = '10.192.0.34';
var_dump( strtok( $ip, '.' ) . '.' . strtok( '.' ) ); // '10.192'


--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)

Thomas 'PointedEars' Lahn

unread,
Mar 28, 2015, 5:49:54 PM3/28/15
to
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.

Thomas 'PointedEars' Lahn

unread,
Mar 28, 2015, 5:52:50 PM3/28/15
to
Thomas Mlynarczyk wrote:

> $ip = '10.192.0.34';
> var_dump( strtok( $ip, '.' ) . '.' . strtok( '.' ) ); // '10.192'

Very nice, thank you.

bird

unread,
Mar 31, 2015, 4:52:22 AM3/31/15
to
(english now :-)

>28/03/2015 22:47, Thomas 'PointedEars' Lahn wrote :
> Lew Pitcher wrote:
> //.....
> 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.
>//....

There is a
");
just under
fwrite($fp, "$time $ip[0].$ip[1] $page $browser");

it seems to be a correct "newline" regarding results :

2015-03-29 03:35 ip.ip.ip.ip / Mozilla/5.0 (iPhone... etc
2015-03-29 03:35 ip.ip.ip.ip /index.php?page=Accueil Mozilla/5.0 ... etc

but now I have
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE,
expecting T_STRING or T_VARIABLE or T_NUM_STRING in
/..../lionwiki/index.php on line 50

line 50:$self = basename($_SERVER['SCRIPT_NAME']);

.htaccess as "php 1"

Jerry Stuckle

unread,
Mar 31, 2015, 9:16:26 AM3/31/15
to
Are you saying the code is actually

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

? If so, you need to post it EXACTLY AS WRITTEN. White space in
strings is important!

If not, then you must have another fwrite which adds a newline character
further down your code.

As for this problem - it is almost assuredly mismatched single quotes
higher up in our code.

This is where a syntactically intelligent editor comes in handy - you
can easily find things like this.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstu...@attglobal.net
==================

Thomas 'PointedEars' Lahn

unread,
Mar 31, 2015, 4:30:33 PM3/31/15
to
bird wrote:

>> 28/03/2015 22:47, Thomas 'PointedEars' Lahn wrote :
>> Lew Pitcher wrote:
>> //.....
>> 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.
>>//....
>
> There is a
> ");
> just under
> fwrite($fp, "$time $ip[0].$ip[1] $page $browser");

If so …

> it seems to be a correct "newline" regarding results :
>
> 2015-03-29 03:35 ip.ip.ip.ip / Mozilla/5.0 (iPhone... etc
> 2015-03-29 03:35 ip.ip.ip.ip /index.php?page=Accueil Mozilla/5.0 ... etc

… this would not be possible because a standalone “")” is a syntax error.
Are you sure those lines are not from a previous run?

> but now I have
> Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE,
> expecting T_STRING or T_VARIABLE or T_NUM_STRING in
> /..../lionwiki/index.php on line 50
>
> line 50:$self = basename($_SERVER['SCRIPT_NAME']);

Assuming that “line 50:” is not in the original code, there is no syntax
error on this line. The reason for the problem must be before that line.

> .htaccess as "php 1"

Impossible to say what you did wrong if you do not post the offending code
*verbatim* (indentation preserved, no prefixes or suffixes added) trimmed
down to the relevant lines.

Please post using your real name.
0 new messages