Tuesday 31 December 2019

How to create and manage log file in php?


Create and manage crud operation log data PHP

<?php
function crud_log($action = '',$message = '',$array_data = array()){
    global $handle_crud_log;
    date_default_timezone_set("Australia/Sydney"); //Ex. set australia/sydney timezone   
    $crud_log_dir = "crud-log"; //Create this DIR(crud-log) on file dir location
   
    if (!file_exists($crud_log_dir)){
        // create directory/folder uploads.
        mkdir($crud_log_dir, 755, true);
    }   

    $quote_log_file_name =  "/crud_log_".date("m_Y").".log";
    $ip_address = @$_SERVER['REMOTE_ADDR']." #### ". @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $date_time = date("d/m/Y H:i:s");
    $action_name = $action; // (Ex. Create/Read/Update/Delete)
    $crud_msg = $message; //Create, Read, Update and Delete message;
    $array_data = $array_data; //Array data type Like $_POST
       
    $handle_crud_log = fopen($crud_log_dir.$quote_log_file_name, "a");
    fwrite($handle_crud_log, "=================================================\r\n");   
    fwrite($handle_crud_log, "At: {$date_time}\r\n");
    fwrite($handle_crud_log, "From: {$ip_address}\r\n");
    fwrite($handle_crud_log, "Action: {$action_name} \r\n");
    fwrite($handle_crud_log, "Message: {$crud_msg}\r\n");
    fwrite($handle_crud_log, "Data: ". print_r($array_data, true));
    fwrite($handle_crud_log, "=================================================\r\n\r\n");
}
$action = "Update";
$message = "Update emplyee detail";
$array_data = array("f_name"=>"PHP", "l_name"=>"Kishan");
crud_log($action, $message, $array_data); // call function for create log
?>

Saturday 14 December 2019

PHP Run Script For Specific IP Address


<?php
// Write your IP instead of xxx.xxx.xxx.xxx

if(@$_SERVER['REMOTE_ADDR'] == 'xxx.xxx.xxx.xxx'|| @$_SERVER['HTTP_X_FORWARDED_FOR'] == 'xxx.xxx.xxx.xxx' ){
    define( 'TESTINGMOD',TRUE);
}else{
    define( 'TESTINGMOD',FALSE);
}

if(TESTINGMOD){
    error_reporting(E_ALL);    ini_set('display_errors', 1);
    // Write test code for specific IP address in live site
}else{
    error_reporting(0);
}
?>