English 中文(简体)
第二例文字与第一例相同的代码
原标题:Second instance of script runs the exact same code as the first one
  • 时间:2012-04-12 09:31:26
  •  标签:
  • php

该书应使用文件锁等书写记录文档,以确保同时操作的文字不会出现任何阅读/仪式的复杂情况。 我从别处搬走。 当我同时两次尝试管理时,我注意到,它完全忽视了24小时档案。 然而,当我连续管理他们时,24小时的档案只是罚款。

这没有任何意义。 如果档案存在,则该书仅作检查,并据此行事。 无论是否使用另一条文字,都不应影响它。 我进行了两次检查,以确保这两起案件都建立了锁档案。

因此,我开始做一些测试。

首先在<代码>11:2-1<>>/代码>产出上开始:

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

第二例始于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

因此,这里有两点错误。 当时的情况,以及该手法篡改锁档案的事实,即使肯定是存在的。

几乎可以说,文字的二例只是产出第一例。

<?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) . "	" . date( H:i:s , $curtime) .  "	";
    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  . "
" .
             #Fields: date time c-ip x-msg  . "
";
            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) . "	" . 
        date( H:i:s , $curtime) .  "	" .
        (isset($_SERVER[ REMOTE_ADDR ]) ? $_SERVER[ REMOTE_ADDR ] :  - ) . "	" .
         "  . str_replace( " ,  "" , $message) .  "  . "
";
        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");
?>

<><>UPDATE:

这里有一个简单的文字,只显示时间。 我在一个不同的东道国里尝试过,因此我认为这不会给我的服务器造成问题。

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

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

Weblog_debug("OK");
?>

再者,如果我开始第二次发言,而第一次发言正同时进行,第二稿将与第一稿同时提出。

问题回答

我可以 believe心地相信自己,但是,这只是一种运行中的“形象”。 文字按原样在>。 我希望我测试的是,在我之前,我只去了所有信使,但是有ya。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签