Rumah  >  Artikel  >  php教程  >  PHP命令行登录系统,标准输入输出,对文件数据增删改查

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

PHP中文网
PHP中文网asal
2016-05-24 12:53:021451semak imbas

PHP命令行登录系统,新用户保存到文件,使用标准输入输出流,对文件中的用户增删改查

<?php
$fp = fopen(&#39;user.log&#39;, &#39;a+&#39;);
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(&#39;php://stdin&#39;,&#39;r&#39;);
$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(&#39;php://stdin&#39;,&#39;r&#39;);
$username=trim(fgets($stdin,100));
fclose($stdin);

fwrite(STDOUT, "请输入密码:");
$stdin=fopen(&#39;php://stdin&#39;,&#39;r&#39;);
$password=trim(fgets($stdin,100));
fclose($stdin);
$content="username:".$username." password:".$password;

$fp = fopen(&#39;user.log&#39;, &#39;r&#39;);
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(&#39;php://stdin&#39;,&#39;r&#39;);
$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(&#39;user.log&#39;, &#39;r&#39;);
$i=1;
while (!feof($fp))
{
$user=fgets($fp);
$username=substr($user, strpos($user,&#39;username:&#39;), strpos($user,&#39;password:&#39;));
if($username){
fwrite(STDOUT, $i++.&#39;. &#39;.$username."\n");
}
}
fwrite(STDOUT, "\n---请输入操作\n");
fwrite(STDOUT, "---1.返回主菜单\n");
fwrite(STDOUT, "---0.退出系统\n");
fwrite(STDOUT, "请输入编号:\n");

$stdin=fopen(&#39;php://stdin&#39;,&#39;r&#39;);
$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(&#39;php://stdin&#39;,&#39;r&#39;);
$username=trim(fgets($stdin,100));
fclose($stdin);

fwrite(STDOUT, "请输入密码:");
$stdin=fopen(&#39;php://stdin&#39;,&#39;r&#39;);
$password=trim(fgets($stdin,100));
fclose($stdin);
$content="username:".$username." password:".$password;

$fp = fopen(&#39;user.log&#39;, &#39;r&#39;);
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(&#39;user.log&#39;, $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(&#39;php://stdin&#39;,&#39;r&#39;);
$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(&#39;php://stdin&#39;,&#39;r&#39;);
$username=trim(fgets($stdin,100));
fclose($stdin);

fwrite(STDOUT, "请输入原密码:");
$stdin=fopen(&#39;php://stdin&#39;,&#39;r&#39;);
$password=trim(fgets($stdin,100));
fclose($stdin);

$content="username:".$username." password:".$password;

$user=file_get_contents(&#39;user.log&#39;);
if($user)
{
if(strpos($user,$content)!==false){
fwrite(STDOUT, "请输入新密码:");
$stdin=fopen(&#39;php://stdin&#39;,&#39;r&#39;);
$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(&#39;user.log&#39;, $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(&#39;php://stdin&#39;,&#39;r&#39;);
$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(&#39;php://stdin&#39;,&#39;r&#39;);
$username=trim(fgets($stdin,100));
fclose($stdin);

$content="username:".$username;

$user=file_get_contents(&#39;user.log&#39;);
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=&#39;&#39;;
}
$content=$str_1.$str_3;
file_put_contents(&#39;user.log&#39;, $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(&#39;php://stdin&#39;,&#39;r&#39;);
$number=trim(fgets($stdin,100));
fclose($stdin);

switch($number){
case 1:
Menu();
break;
case 2:
DeleteUser();
break;
case 0:
exit("已退出系统!\n");
default :
exit("输入错误,已退出系统!\n");
}
}
Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn