i created simple login php script that check username and password and save it in file log.txt. problem is simple when it wrote to file i need that script write new line each login access: login pass username= user login fail username= test my script is: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>PHP form test comment</title> <!-- define some style elements--> <style> textarea { text-align:left; margin-left:+40px; } input#name{ margin-left:+15px; } input#password{ margin-left:+5px; } input#id{ margin-left:+54px; } input#submit { margin-left:+54px; } </style> </head> <body> <?php $test=""; $list=""; $name=""; $username=""; $password=""; $log="log.txt"; $fd=fopen($log,"a+"); $id=""; $flag=1; //$test = $_POST['name']; //echo $test; if ($_SERVER["REQUEST_METHOD"] == "POST") { $username = test_input($_POST["username"]); if (empty($username)) { echo "Username is required" ;} $password = test_input($_POST["password"]); if (empty($password)) { echo "Password is required" ;} } if (empty($_POST["username"]) || (empty ($_POST["password"]))){ //$login="login_fail"; $login = "login fail\n";} else //$login="login_pass"; $login = "login pass\n"; $login_details=$_POST["username"]; fwrite($fd,$login); fwrite ($fd," "); fwrite($fd,"username="); fwrite($fd," "); fwrite ($fd,$login_details); fwrite ($fd," "); fclose($fd); function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <form id="test" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post"> Username: <input type='text' name='username' id='usename' ><br /> Password: <input type='text' name='password' id='password' ><br /> <input type='submit' value='Submit' id="submit" /> </form> </body> </html> PHP:
Have you tried "a" instead of "a+" in your fopen function? You could also use file_put_contents() $txt = "password".PHP_EOL."username"; $myfile = file_put_contents('log.txt', $txt.PHP_EOL , FILE_APPEND); PHP: Source: http://stackoverflow.com/questions/24972424/php-create-or-write-append-in-text-file
You also don't want to do that to passwords, what you're doing with test_input() - basically, what that does is reduce the complexity of the passwords - granted, I can't see that you're even hashing anything (thus far, at least), hence the passwords are basically a joke anyway, but you shouldn't reduce the complexity for no reason.
function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return substr($data, -1); } PHP: Haha.... But yea, you shouldn't do this. Better to use Blowfish (http://www.the-art-of-web.com/php/blowfish-crypt/) or something to hash your passwords and then check new entries against the stored hash.