How to create a logfile in php
To write to a log file and make a new one each day, you could use date("j.n.Y")
as part of the filename.
//Something to write to txt log
$log = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
"Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
"User: ".$username.PHP_EOL.
"-------------------------".PHP_EOL;
//Save string to log, use FILE_APPEND to append.
file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
So you would place that within your hasAccess()
method.
public function hasAccess($username,$password){
$form = array();
$form['username'] = $username;
$form['password'] = $password;
$securityDAO = $this->getDAO('SecurityDAO');
$result = $securityDAO->hasAccess($form);
//Write action to txt log
$log = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
"Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
"User: ".$username.PHP_EOL.
"-------------------------".PHP_EOL;
//-
file_put_contents('./log_'.date("j.n.Y").'.txt', $log, FILE_APPEND);
if($result[0]['success']=='1'){
$this->Session->add('user_id', $result[0]['id']);
//$this->Session->add('username', $result[0]['username']);
//$this->Session->add('roleid', $result[0]['roleid']);
return $this->status(0,true,'auth.success',$result);
}else{
return $this->status(0,false,'auth.failed',$result);
}
}
Create a log file in PHP
For what it's worth MySQL provides the very efficient ARCHIVE
storage engine, suitable for write-a-lot read-infrequently use patterns.
It's a good choice if you want your log data in the DBMS (backed up, etc).
It's also a good choice for log data when your web app runs on multiple servers: all the entries go to one place, and you don't have to wonder which machine a particular log entry lives on.
Using php to write to a log file
Why not just use error_log()
? With the message_type
set to 3
(second parameter) the message will be written the the file specified in the third parameter:
$message = $_GET['w'];
$logfile = 'logfile.log';
// Debug: A line for verifying I have the message
echo "Writing message '$message' to logfile<br>";
error_log($message."\n", 3, $logfile);
// Debug: read back the log file to verify thatthe line has been written
readfile($logfile);
Note the newline
appended to the message as error_log()
doesn't do this for you.
Note also that permissions must be set to allow the web server to write to the target file. This is true whether using error_log()
or file_put_contents()
PHP reference is here
How to create a log file in PHP
error_log("You messed up!".$my_message, 3, "/var/tmp/custom-errors.log");
Create or write/append in text file
Try something like this:
$txt = "user id date";
$myfile = file_put_contents('logs.txt', $txt.PHP_EOL , FILE_APPEND | LOCK_EX);
Creating A log File every day By php and Javascript
Crate one more variable like the $date one:
$newTime = date('dmY');
And then add this in your file name:
$file = "/home/shakilofficial/public_html/vtinfo/mainindex{$newTime}.html";
So you will have a different file every day with different name
how to create a log file in code igniter?
I believe you should have an automatic generated error log which is stored in your root folder.
Check for a file called "php_errorlog" or something similar to this.
You can then download it and open it with Notepad or any other editor.
Php code to Generate Log File
<?php
$filename = "logs.php";
$lines = file($filename);
if(count($lines)!=0){
$data = explode( " :- ", $lines[count($lines) -1 ]);
if(date('d-M-y') == $data[0]){
$data[1]++;
$lines[count($lines) -1 ] = $data[0].' :- '. $data[1];
}else{
$lines[] = date('d-M-y').' :- 1';
}
}else{
echo 'Empty';
$lines[] = date('d-M-y').' :- 1';
}
file_put_contents($filename, implode("\n", $lines ) );
?>
Related Topics
PHP Allocate Color Without Image Resource
PHP Get Actual Maximum Upload Size
Strip PHP Variable, Replace White Spaces with Dashes
Call ASP.NET Web Service from PHP with Multiple Parameters
Using Zend Framework for Highload Projects
Setting Up a Cronjob in Windows Xampp
Setting Environment Variables in Mamp
Sending Sms with Amazon Aws Services PHP
Object Oriented PHP Best Practices
How to Retrieve All Variables from a Twig Template
Php: What's the Point of Upload_Err_Ini_Size
Passing Value from PHP Script to Python Script
Dyld: Library Not Loaded: /Usr/Local/Lib/Libjpeg.8.Dylib - Homebrew PHP
PHP Regex to Remove Http:// from String
Upload Multiple Images with Jquery Ajax and Process Them with PHP
Phpexcel_Writer_Exception with Message "Could Not Close Zip File PHP://Output."