异步请求 - 用户数据 - session实战
login文档中doLogin函数
handle文档
async function doLogin(obj){
const email = obj.form.email.value;
const password = obj.form.password.value;
if (email.length > 0 && password.length > 0){
const response = await fetch('./lib/user/handle.php', {
method:'POST',
headers: {
'Content-type':'application/json;charset=utf-8'
},
body:JSON.stringify({
email,
password}),
});
const result = await response.json();
if(result){
alert('登录成功!');
location.href = 'index.php';
}else{
alert('验证失败!');
location.href = 'login.php';
}
}
}
header登录按钮
session_start();
$users = require __DIR__. '/../../data/user.php';
$json = file_get_contents('php://input');
$newUser = json_decode($json, true);
$salt = 'dmegc';
$email = $newUser['email'];
$password = md5($salt . $newUser['password']);
$newArr2 = array_values(array_filter([1,2,3,4,5,6], function($item){
return $item%2==0;
}));
$result = array_values(array_filter($users, function($user) use ($email, $password){
return $user['password'] === $password && $user['email'] === $email;
}));
$flag = false;
if(count($result)===1){
$flag = true;
$_SESSION['name'] = $result[0]['name'];
}
echo json_encode($flag);
总结:
<a href="login.php"><?php echo isset($_SESSION['name'])?$_SESSION['name'].'注销':'登录/注册'?></a>
挺简单的内容,可以讲的快一点;留出时间多实战。
【文章原创作者:国外高防服务器 http://www.558idc.com/shsgf.html转载请说明出处】