Created
March 13, 2012 15:25
-
-
Save fetus-hina/2029400 to your computer and use it in GitHub Desktop.
http://nemoa.biz/php/data.html のアクセスカウンタープログラム
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// http://nemoa.biz/php/data.html の「アクセスカウンター」プログラム | |
$pointer=fopen("log.cgi", "r"); | |
$line = fgets($pointer); | |
fclose($pointer); | |
$nom = $line +1; | |
$fp = fopen("log.cgi","w"); | |
fputs($fp,$nom); | |
fclose($fp); | |
echo "$nom"; | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ちゃんとロックするように書き直し(体裁はできるだけキープ) | |
// こうしないと、 | |
// ・A さんが fgets() で読んだデータ "42" とほぼ同時に B さんが読んだデータ "42" が存在し、 | |
// ともに自分は "43" 番目だと認識するのでカウント漏れが発生する可能性がある | |
// ・A さんが "43" を書こうと "w" で fopen() したときに log.cgi の中身は 0 バイトに切り詰められるので、 | |
// その後 fputs か fclose (正確なタイミングは知らない)するまでに B さんがデータを読むと、"0" と同等 | |
// に扱われてカウントが吹っ飛ぶ可能性がある。昔から "CGI" (Perl) で作成されているカウンタがよく飛ぶのは | |
// これが原因 | |
// ※42 という数字に意味はない | |
// ファイルロックは正しく行いましょう。 | |
$pointer=fopen("log.cgi", "r+"); | |
flock($pointer, LOCK_EX); | |
$line = fgets($pointer); | |
$nom = $line +1; | |
fseek($pointer, 0, SEEK_SET); | |
fputs($pointer,$nom); | |
fclose($pointer); | |
echo "$nom"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment