Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #15135 > unrolled thread
| Started by | bird <new.bird@free.fr> |
|---|---|
| First post | 2015-03-28 18:48 +0100 |
| Last post | 2015-03-28 22:50 +0100 |
| Articles | 8 — 5 participants |
Back to article view | Back to comp.lang.php
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
| From | bird <new.bird@free.fr> |
|---|---|
| Date | 2015-03-28 18:48 +0100 |
| Subject | dans un log.txt : reduire IP à deux octets selon recomm CNIL |
| Message-ID | <5516e98a$0$2988$426a34cc@news.free.fr> |
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
[toc] | [next] | [standalone]
| From | Lew Pitcher <lew.pitcher@digitalfreehold.ca> |
|---|---|
| Date | 2015-03-28 14:58 -0400 |
| Message-ID | <9RCRw.180861$jx4.20363@fx27.iad> |
| In reply to | #15135 |
On Saturday March 28 2015 13:48, in comp.lang.php, "bird" <new.bird@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
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2015-03-28 22:47 +0100 |
| Message-ID | <13863983.OW5F8XF2sQ@PointedEars.de> |
| In reply to | #15136 |
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.
[toc] | [prev] | [next] | [standalone]
| From | bird <new.bird@free.fr> |
|---|---|
| Date | 2015-03-31 10:52 +0200 |
| Subject | in a log.txt : how convert IP.IP.IP.IP to IP.IP.*.* (hope of french CNIL) ? |
| Message-ID | <551a603a$0$3318$426a74cc@news.free.fr> |
| In reply to | #15138 |
(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"
[toc] | [prev] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2015-03-31 09:16 -0400 |
| Subject | Re: in a log.txt : how convert IP.IP.IP.IP to IP.IP.*.* (hope of french CNIL) ? |
| Message-ID | <mfe6lf$64t$1@dont-email.me> |
| In reply to | #15150 |
On 3/31/2015 4:52 AM, bird wrote: > (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" > 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 jstucklex@attglobal.net ==================
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2015-03-31 22:28 +0200 |
| Subject | Re: in a log.txt : how convert IP.IP.IP.IP to IP.IP.*.* (hope of french CNIL) ? |
| Message-ID | <3564629.l9P8CqJurF@PointedEars.de> |
| In reply to | #15150 |
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. -- PointedEars Zend Certified PHP Engineer Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> |
|---|---|
| Date | 2015-03-28 20:15 +0100 |
| Message-ID | <mf6ukf$2u6$1@news.albasani.net> |
| In reply to | #15135 |
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)
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2015-03-28 22:50 +0100 |
| Message-ID | <3162368.3IHaJqDC24@PointedEars.de> |
| In reply to | #15137 |
Thomas Mlynarczyk wrote: > $ip = '10.192.0.34'; > var_dump( strtok( $ip, '.' ) . '.' . strtok( '.' ) ); // '10.192' Very nice, thank you. -- PointedEars Zend Certified PHP Engineer Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web