Adding an entry to users.log file
12
June
2013

Script allows you to add first and last name to a file (users.log) and then just read it out to the screen. It can be used for creating a guest book without a database.

<?php

define("USERS_LOG","users.log");

if($_SERVER["REQUEST_METHOD"] == "POST"){
$fn = trim(strip_tags($_POST["fname"]));
$ln = trim(strip_tags($_POST["lname"]));
$user = "$fn $ln \n";
file_put_contents(USERS_LOG,$user, FILE_APPEND);
header("Location: test.php");
exit;
}
?>

<form action="" method="post">
<input type='text' name='fname'>
<input type='text' name='lname'>
<input type='submit' value='submit it'>

</form>

<?php
if(file_exists(USERS_LOG)){

$users = file(USERS_LOG);
if(is_array($users)){
$users = array_reverse($users);
echo "<ol>";
foreach ($users as $user){
echo "<li>$user</li>";
}
echo "</ol>";
}

}
?> 
  • 918 View
  • Pin It