批改状态:合格
老师批语:
async function doLogin(obj){ //async 定义为异步函数const email = obj.form.email.valueconst password =obj.form.password.value// console.log(email, password);if(email.length >0 && password.length > 0){// 异步提交fetch api// console.log('111');const res = await fetch('./lib/user/handle.php',{//请求方法method:'post',//设置请求头headers:{'content-type':'application/json;charset=utf-8'},//将需要传送到服务器上的数据,解析为jsonbody:JSON.stringify({email,password})})// 解析返回的数据 response返回json格式时,用r.json()打印响应的内容const result = await res.json()console.log(result);
if(result){alert('登陆成功')location.href='index.php'}else{alert('验证失败')location.href='login.php'}
$json = file_get_contents('php://input');$user = json_decode($json, true);$email = $user['email'];$password = $user['password'];
$result = array_values(array_filter($users,function($user)use($email,$password){return $user['email']===$email&&$user['password']===$password;}));$flag = false;if(count($result)===1){$flag = true;$_SESSION['username'] = $result[0]['name'] ;}echo json_encode($flag);
session_start();$username = $_SESSION['username'];<?php if (isset($username)) :;?><?=$username?> <a href='#'>[注销]</a><?php else:?><a href="login.php">登录</a><?php endif?>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号