0

このスクリプトは、ファイル ロックなどを使用してログ ファイルを書き込み、同時に実行されているスクリプトが読み取り/書き込みの問題を起こさないようにすることになっています。私はphp.netの誰かからそれを手に入れました。同時に 2 回実行しようとすると、ロック ファイルが完全に無視されることに気付きました。ただし、それらを連続して実行すると、ロックファイルは問題なく機能しました。

それはまったく意味がありません。スクリプトは、ファイルが存在するかどうかを確認し、それに基づいて動作します。別のスクリプトが実行されているかどうかに関係なく、まったく影響を受けません。どちらの場合も、ロック ファイルが作成されていることを再確認しました。そうだった。

それで、私はいくつかのテストを始めました。

出力で開始された最初のインスタンス11:21:00:

Started at: 2012-04-12 11:21:00 
Checking if weblog/20120412test.txt.1.wlock exists
Got lock: weblog/20120412test.txt.1.wlock
log file not exists, make new
log file was either appended to or create anew
Wrote: 2012-04-12 11:21:00 xx.xx.xx.xxx "testmsg" 
1

出力で開始された 2 番目のインスタンス11:21:03:

Started at: 2012-04-12 11:21:00 
Checking if weblog/20120412test.txt.1.wlock exists
Got lock: weblog/20120412test.txt.1.wlock
log file not exists, make new
log file was either appended to or create anew
Wrote: 2012-04-12 11:21:00 xx.xx.xx.xxx "testmsg" 
1

したがって、ここには 2 つの問題があります。タイムスタンプ、およびスクリプトがロックファイルが存在しないと言っているという事実は、それが最も確実に存在するにもかかわらずです。

スクリプトの 2 番目のインスタンスは、最初のインスタンスが行ったことを単純に出力するかのようです。

<?php
function Weblog_debug($input)
{
    echo $input."<br/>";
}
function Weblog($directory, $logfile, $message)
{
    // Created 15 september 2010: Mirco Babin
    $curtime = time();

    $startedat = date('Y-m-d',$curtime) . "\t" . date('H:i:s', $curtime) .  "\t";
    Weblog_debug("Started at: $startedat");

    $logfile = date('Ymd',$curtime) . $logfile;

    //Set directory correctly
    if (!isset($directory) || $directory === false)
    $directory = './';
    if (substr($directory,-1) !== '/')
    $directory = $directory . '/';

    $count = 1;
    while(1)
    {
        //*dir*/*file*.*count*
        $logfilename = $directory . $logfile . '.' . $count;

        //*dir*/*file*.*count*.lock
        $lockfile = $logfilename . '.wlock';
        $lockhandle = false;
        Weblog_debug("Checking if $lockfile exists");
        if (!file_exists($lockfile))
        {
            $lockhandle = @fopen($lockfile, 'xb'); //lock handle true if lock file opened
            Weblog_debug("Got lock: $lockfile");
        }
        if ($lockhandle !== false) break; //break loop if we got lock

        $count++;
        if ($count > 100) return false;
    }

    //log file exists, append
    if (file_exists($logfilename))
    {
        Weblog_debug("log file exists, append");
        $created   = false;
        $loghandle = @fopen($logfilename, 'ab');
    }
    //log file not exists, make new
    else
    {
        Weblog_debug("log file not exists, make new");
        $loghandle = @fopen($logfilename, 'xb');
        if ($loghandle !== false) //Did we make it?
        {
            $created = true;

            $str = '#version: 1.0' . "\r\n" .
            '#Fields: date time c-ip x-msg' . "\r\n";
            fwrite($loghandle,$str);
        }
    }

    //was log file either appended to or create anew?
    if ($loghandle !== false)
    {
        Weblog_debug("log file was either appended to or create anew");
        $str = date('Y-m-d',$curtime) . "\t" . 
        date('H:i:s', $curtime) .  "\t" .
        (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '-') . "\t" .
        '"' . str_replace('"', '""', $message) . '"' . "\r\n";
        fwrite($loghandle,$str);

        Weblog_debug("Wrote: $str");

        fclose($loghandle);
        //Only chmod if new file
        if ($created) chmod($logfilename,0644); // Read and write for owner, read for everybody else

        $result = true;
    }
    else
    {
        Weblog_debug("log file was not appended to or create anew");
        $result = false;
    }

    /**
    Sleep & disable unlinking of lock file, both for testing purposes.
    */
    //Sleep for 10sec to allow other instance(s) of script to run while this one still in progress.
    sleep(10);
    //fclose($lockhandle);
    //@unlink($lockfile);

    return $result;
}

echo Weblog("weblog", "test.txt", "testmsg");
?>

アップデート:

タイムスタンプを表示するだけの簡単なスクリプトを次に示します。別のホストで試したので、サーバーの問題ではないと思います。

<?php
function Weblog_debug($input)
{
    echo $input."<br/>";
}
$curtime = time();
$startedat = date('Y-m-d',$curtime) . "\t" . date('H:i:s', $curtime) .  "\t";
Weblog_debug("Started at: $startedat");

$timediff = time() - $curtime;
while($timediff < 5)
{
    $timediff = time() - $curtime;
}

Weblog_debug("OK");
?>

繰り返しますが、最初のスクリプトが while ループ内にある間にスクリプトの 2 番目のインスタンスを開始すると、2 番目のスクリプトは最初のスクリプトと同時に開始したと表示します。

4

1 に答える 1

0

私はこれを信じられませんが、これは Opera の単なる「機能」であることがわかりました。スクリプトは Firefox で意図したとおりに機能します。私はこれについてすべて凶暴になる前にそれをテストしたかったのですが、そこに行きます.

于 2012-04-13T14:34:53.010 に答える