Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
<?php
// 开启会话
session_start();
$allowOpts=['login','register','logout'];
// 1.导入用户数据
require __DIR__.'/../../config/common.php';
$users=require DATA_PATH.'/users.php';
// 2.获取get数据
$action=$_GET['action'];
// 3,允许值过滤
$allow=array_filter($allowOpts,function($allow)use($action){ return $allow===$action;});
switch (count($allow)) {
//---------------1.登录模块--------//
case $action==='login':
$email=$_GET['email'];
$password = md5($_GET['password']);
$result = array_filter($users,function($user) use ($email,$password){ return $user['email']=== $email&&$user['password'] === $password; });
$prompt=false;
if(count($result)===1){$prompt=true;$_SESSION['user'] =array_pop($result);}
echo json_encode($prompt);
break;
//---------------1.登录模块--------//
//---------------2.用户注册模块--------//
case $action==='register':
// 1).数组长度
$oriCount = count($users);
//2).接收数据
$json = file_get_contents('php://input');
//3).json转数组
$user = json_decode($json,true);
// 4).创建新用户数据
$user['password'] = md5($user['password']);
$user['id'] = count($users)+1;
//5).添加新用户
$users[]=$user;
// 6).分析结果
$prompt=false;
if(count($users)===$oriCount+1){$prompt=true;}
echo json_encode($prompt);
break;
//---------------2.用户注册模块--------//
//---------------3.用户退出模块--------//
case $action==='logout':
$prompt=false;
if(session_destroy()){ $prompt=true;}
echo json_encode($prompt);
break;
//---------------3.用户退出模块--------//
default:
echo '请求错误';
break;
}