PHP命令行登录系统,新用户保存到文件,使用标准输入输出流,对文件中的用户增删改查
<?php $fp = fopen('user.log', 'a+'); Menu(); function Menu(){ fwrite(STDOUT, "----------欢迎进入登录系统----------\n"); fwrite(STDOUT, "---1.登录系统\n"); fwrite(STDOUT, "---2.查看用户\n"); fwrite(STDOUT, "---3.添加用户\n"); fwrite(STDOUT, "---4.修改密码\n"); fwrite(STDOUT, "---5.注销用户\n"); fwrite(STDOUT, "---0.退出系统\n"); fwrite(STDOUT, "请输入编号:\n"); $stdin=fopen('php://stdin','r'); $number=trim(fgets($stdin,100)); fclose($stdin); switch($number){ case 1: Login(); break; case 2: ViewUser(); break; case 3: AddUser(); break; case 4: ChangePassword(); break; case 5: DeleteUser(); break; case 0: exit("已退出系统!\n"); default : fwrite(STDOUT, "输入错误,请重新输入!\n"); Menu(); break; } } function Login(){ fwrite(STDOUT, "\n---登录系统---\n"); fwrite(STDOUT, "请输入用户名:"); $stdin=fopen('php://stdin','r'); $username=trim(fgets($stdin,100)); fclose($stdin); fwrite(STDOUT, "请输入密码:"); $stdin=fopen('php://stdin','r'); $password=trim(fgets($stdin,100)); fclose($stdin); $content="username:".$username." password:".$password; $fp = fopen('user.log', 'r'); while (!feof($fp)) { $user=fgets($fp); if(strpos($user,$content)===0){ fwrite(STDOUT, "\n登录成功!\n\n"); $user_look_=true; break; }else{ $user_look_=false; } } if($user_look_===false){ fwrite(STDOUT, "\n登录失败!\n\n"); } fwrite(STDOUT, "---请输入操作\n"); fwrite(STDOUT, "---1.返回主菜单\n"); fwrite(STDOUT, "---2.重新登录\n"); fwrite(STDOUT, "---0.退出系统\n"); fwrite(STDOUT, "请输入编号:\n"); $stdin=fopen('php://stdin','r'); $number=trim(fgets($stdin,100)); fclose($stdin); switch($number){ case 1: Menu(); break; case 2: Login(); break; case 0: exit("已退出系统!\n"); default : exit("输入错误,已退出系统!\n"); break; } } function ViewUser(){ fwrite(STDOUT, "\n---显示用户---\n"); $fp = fopen('user.log', 'r'); $i=1; while (!feof($fp)) { $user=fgets($fp); $username=substr($user, strpos($user,'username:'), strpos($user,'password:')); if($username){ fwrite(STDOUT, $i++.'. '.$username."\n"); } } fwrite(STDOUT, "\n---请输入操作\n"); fwrite(STDOUT, "---1.返回主菜单\n"); fwrite(STDOUT, "---0.退出系统\n"); fwrite(STDOUT, "请输入编号:\n"); $stdin=fopen('php://stdin','r'); $number=trim(fgets($stdin,100)); fclose($stdin); switch($number){ case 1: Menu(); break; case 0: exit("已退出系统!\n"); default : exit("输入错误,已退出系统!\n"); break; } } function AddUser(){ fwrite(STDOUT, "\n---添加用户---\n"); fwrite(STDOUT, "\n请输入用户名:"); $stdin=fopen('php://stdin','r'); $username=trim(fgets($stdin,100)); fclose($stdin); fwrite(STDOUT, "请输入密码:"); $stdin=fopen('php://stdin','r'); $password=trim(fgets($stdin,100)); fclose($stdin); $content="username:".$username." password:".$password; $fp = fopen('user.log', 'r'); while (!feof($fp)) { $user=fgets($fp); if(strpos($user,$content)===0){ fwrite(STDOUT, "\n用户已存在!\n\n"); $user_look_=true; break; }else{ $user_look_=false; } } if($user_look_===false){ file_put_contents('user.log', $content . "\r\n", FILE_APPEND); fwrite(STDOUT, "\n用户添加成功!\n\n"); } fwrite(STDOUT, "---请输入操作\n"); fwrite(STDOUT, "---1.返回主菜单\n"); fwrite(STDOUT, "---2.添加用户\n"); fwrite(STDOUT, "---0.退出系统\n"); fwrite(STDOUT, "请输入编号:\n"); $stdin=fopen('php://stdin','r'); $number=trim(fgets($stdin,100)); fclose($stdin); switch($number){ case 1: Menu(); break; case 2: AddUser(); break; case 0: exit("已退出系统!\n"); default : exit("输入错误,已退出系统!\n"); } } function ChangePassword(){ fwrite(STDOUT, "\n---修改密码---\n"); fwrite(STDOUT, "\n请输入用户名:"); $stdin=fopen('php://stdin','r'); $username=trim(fgets($stdin,100)); fclose($stdin); fwrite(STDOUT, "请输入原密码:"); $stdin=fopen('php://stdin','r'); $password=trim(fgets($stdin,100)); fclose($stdin); $content="username:".$username." password:".$password; $user=file_get_contents('user.log'); if($user) { if(strpos($user,$content)!==false){ fwrite(STDOUT, "请输入新密码:"); $stdin=fopen('php://stdin','r'); $password1=" password:".trim(fgets($stdin,100)); fclose($stdin); $content=substr_replace($user,$password1,strpos($user,"username:".$username)+strlen("username:".$username),strlen(" password:".$password)); file_put_contents('user.log', $content); $user_look_=true; }else{ $user_look_=false; } }else{ $user_look_=false; } if($user_look_===false){ fwrite(STDOUT, "\n该用户不存在!\n\n"); } fwrite(STDOUT, "\n---请输入操作\n"); fwrite(STDOUT, "---1.返回主菜单\n"); fwrite(STDOUT, "---2.修改密码\n"); fwrite(STDOUT, "---0.退出系统\n"); fwrite(STDOUT, "请输入编号:\n"); $stdin=fopen('php://stdin','r'); $number=trim(fgets($stdin,100)); fclose($stdin); switch($number){ case 1: Menu(); break; case 1: ChangePassword(); break; case 0: exit("已退出系统!\n"); default : exit("输入错误,已退出系统!\n"); break; } } function DeleteUser(){ fwrite(STDOUT, "\n---删除用户---\n"); fwrite(STDOUT, "\n请输入用户名:"); $stdin=fopen('php://stdin','r'); $username=trim(fgets($stdin,100)); fclose($stdin); $content="username:".$username; $user=file_get_contents('user.log'); if($user) { if(strpos($user,$content)!==false){ $str_1=substr($user, $start = 0, strpos($user,"username:".$username)); $str_2=substr($user, strpos($user,"username:".$username)+strlen("username:".$username)); if(strpos($str_2,"username:")){ $str_3=substr($str_2, strpos($str_2,"username:")); }else{ $str_3=''; } $content=$str_1.$str_3; file_put_contents('user.log', $content); $user_look_=true; }else{ $user_look_=false; } }else{ $user_look_=false; } if($user_look_===false){ fwrite(STDOUT, "\n用户不存在!\n\n"); } fwrite(STDOUT, "---请输入操作\n"); fwrite(STDOUT, "---1.返回主菜单\n"); fwrite(STDOUT, "---2.删除用户\n"); fwrite(STDOUT, "---0.退出系统\n"); fwrite(STDOUT, "请输入编号:\n"); $stdin=fopen('php://stdin','r'); $number=trim(fgets($stdin,100)); fclose($stdin); switch($number){ case 1: Menu(); break; case 2: DeleteUser(); break; case 0: exit("已退出系统!\n"); default : exit("输入错误,已退出系统!\n"); } }