PHP寫文本日誌

/**

* 寫文件
* @param    string  $file   文件路徑
* @param    string  $str    寫入內容
* @param    char    $mode   寫入模式
*/
function writeFile($file,$str,$mode='w')
{
    $oldmask = @umask(0);
    $fp = @fopen($file,$mode);
    @flock($fp, 3);
    if(!$fp)
    {
        Return false;
    }
    else
    {
        @fwrite($fp,$str);
        @fclose($fp);
        @umask($oldmask);
        Return true;
    }
}

 

擴展應用,比如記錄每次請求的url內容

function writeGetUrlInfo()
{

  //獲取請求方的地址,客戶端,請求的頁面及參數
   $requestInformation = $_SERVER['REMOTE_ADDR'].', '.$_SERVER['HTTP_USER_AGENT'].', http://'.$_SERVER['HTTP_HOST'].htmlentities        ($_SERVER['PHP_SELF']).'?'.$_SERVER['QUERY_STRING']."\n"; 
   $fileName = RootPath.'/log/'.date('Y-m-d').'.log'; //網站根目錄RootPath是在配置文件裏define('RootPath', substr(dirname(__FILE__))); 
   writeFile($fileName, $requestInformation, 'a'); //表示追加
}


orther Eg:

 $date = date("Y-m-d",time());
        $time = date('H:i:s',time());
        $dir = root."data/GamePay/{$paycord}/";
        isdir($dir,0777);
        $payfile = $dir."pay_$date.log";
        $fp = @fopen($payfile,'a+');
        if($fp){      
            @fwrite($fp,"$date $time member_id:$member_id\n");
            @fwrite($fp,"$date $time username:$username\n");
            @fwrite($fp,"$date $time order_id:$order_id"\n");
            @fwrite($fp,"$date $time gold:$gold\n");
            @fwrite($fp,"$date $time url:$url\n\n");          
            fclose($fp);
        }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章