PHP命令行登录系统,标准输入输出,对文件数据增删改查

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");
}
}

打赏此文

如果您觉得本站的内容对您有所帮助,您可以扫描下面的二维码小额支付请我喝杯茶,感谢!打赏记录
支付宝
微信
承诺:凡打赏捐助的朋友,留言备注自己的邮箱,在打赏捐助时间点的6个月内,本站会每周邮件推送原创专业技术博文,供大家学习和参考!

6 条评论

  1. starlight说道:

    代码不错,直接就可以运行,正在找这种php对文件的操作代码

  2. 大牛说道:

    对于我这种菜鸟,刚合适

  3. 漂洋过海说道:

    很实用,博主大神求带啊!

  4. 请叫我招财猫说道:

    受教了。很好的借鉴

  5. PZ ..说道:

    写的好

  6. 刘宽说道:

    写的东西很不错

留下评论

All fields marked (*) are required