0

Hello I am trying to create an audit log when a user enters through my log in.

The idea is that the audit.php creates a log.txt and add user history when a user logs in.

audit.php:

<?php
function logger($log){
if(!file_exists('log.txt')){
    file_put_contents('log.txt','');
}
$ip = $_SERVER['REMOTE_ADDR']; //Client IP
$time = date('m/d/y h:iA',time());
$contents = file_get_contents('log.txt');
$contents .= "$ip\t$time\t$log\r";

file_put_contents('log.txt',$contents);
}    
?>    

I am Trying to implement this into my login code.

login.php:

if ($_SERVER[ 'REQUEST_METHOD' ] == 'POST')
{
$usernane = $_POST['username'];
$password = ($_POST['password' ]);
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ? AND password = ? LIMIT 1");
$stmt->execute(array($usernane, $password));
$checkuser = $stmt->rowCount();
$user = $stmt->fetch();



if ($checkuser === 0){
}else if($checkuser === 1){ 
    $_SESSION[ 'user' ] = $user['username'];
    $_SESSION[ 'type'] = $user['type'];
if($user['type'] == 1){ 
    header('location:Admin.php');
}else{

I tried implementing it in this portion of the code.

It comes up with the error " Uncaught Error: Call to undefined function logger()"

Any help is appreciated!

  $log = "User Entered The Home Page";
  logger($log);
    header('location:Home.php');
}

}
}
DarkBee
  • 16,592
  • 6
  • 46
  • 58
  • 1
    Did you include/require the file `audit.php`? – DarkBee Jan 12 '22 at 06:51
  • I have not. How is that implemented? – Malphite_User Jan 12 '22 at 06:53
  • Read the documentation: [`require`](https://www.php.net/manual/en/function.require.php). Also, you should call [`exit`](https://www.php.net/manual/en/function.exit.php) after any `header('Location')` - the script will still continue to run past the `header` execution - unless you want this to happen. Future reading: [PHP-PSR4 with composer to autoload dependencies](https://code.tutsplus.com/tutorials/how-to-autoload-classes-with-composer-in-php--cms-35649) – Jaquarh Jan 12 '22 at 06:54
  • Will do. Thank You – Malphite_User Jan 12 '22 at 06:55
  • 1
    Future reading: [`password_hash`](https://www.php.net/manual/en/function.password-hash.php) and [`password_verify`](https://www.php.net/manual/en/function.password-verify.php) - You should never store plaintext credentials, even if "not needed" - stick to "best practice" – Jaquarh Jan 12 '22 at 07:02
  • Does this answer your question? [How do I include other files in PHP?](https://stackoverflow.com/questions/47713287/how-do-i-include-other-files-in-php) – Jaquarh Jan 12 '22 at 07:08
  • Thank You for the tips! the code is good now! Thank You! – Malphite_User Jan 12 '22 at 07:24

0 Answers0