<form action="index.php" method="post"> <input type="text" name="user"/> <input type="text" name="password"/> <input type="submit" value="submit"/> </form>
<?php $user=trim($_POST['user']); $password=trim($_POST['password']); if(!$user || !$password){ echo 'please make sure no empty input'; exit; } //write to test.txt $handle=fopen('test.txt','ab+'); if($handle){ $exits=false; while(($buffer=fgets($handle))!==false){ $match=preg_match('/.*:(.*?)-/',$buffer,$array); //first write if(!$match){ break; } //exist in txt if($match && $array[1]==$user){ $exits=true; } } }else{ echo 'fail to open file'; } if(!$exits){ fwrite($handle,'$user:'.$user.'-----'.'$password:'.$password."\r\n"); }else{ echo 'user exist'; } fclose($handle); ?>
本文介绍了一个使用PHP实现的简单登录表单,包括输入验证和将用户信息写入本地文件的功能。通过分析代码,可以了解如何接收表单数据、进行基本的输入检查并将其保存到txt文件中。
320

被折叠的 条评论
为什么被折叠?



