Tuesday 31 March 2015

Create a Random String In PHP

$characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$randstring = '';
for ($i = 0; $i < 10; $i++) {
$randstring .= $characters[rand(0, strlen($characters))];
}
echo $randstring;

Generating a Log File In PHP

$log = "-----------Chinnayya Naidu Nalla's Application Accesed on".date("Y-m-d H:i:s"). "--------------".PHP_EOL.
"User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
"-------------------------".PHP_EOL;
file_put_contents('./log_'.date("j.n.Y").'.txt', $log, FILE_APPEND);





Place this code in your Php code it will generate a log file in your application. to achieve polymorphic nature place this in a function in a separate php file and include it. remember to create a function with a parameter to pass the log message to it Logging Code in a function with a parameter


function WriteLog($logmessage){
file_put_contents('./log_'.date("j.n.Y").'.txt', $logmessage, FILE_APPEND); }