Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
首先在主页表头添加点击事件,logout方法
async function logout(){
if (confirm('是否退出')) {
// const url = './lib/userHandle.php?action=logout';
const url = './lib/user/logout.php';
const response = await fetch(url);
const result = await response.json();
if(result){
alert('退出成功');
location.href='index.php';
}else{
alert('退出失败');
location.href='login.php';
}
}
}
在logout文件中销毁session
session_start();
$flag = false;
if(session_destroy()){
$flag = true;
}
echo json_decode($flag);
表单验证主要有获取数据,非空验证,两次密码相同验证,插入数据等几步,可分别用函数验证;数据插入部分尚未完成;
function addUser(btn){
const user = getInput(btn.form)
alert(JSON.stringify(user));
if(isEmpty(user)){
if(isPswEqu(user)){
const data = createData(user)
insertUser(data);
}
}
}
const getInput = (form) =>{
return {
uname:{
ele:form.uname,
value:form.uname.value.trim()
}, email:{
ele:form.email,
value:form.email.value.trim()
},psw1:{
ele:form.psw1,
value:form.psw1.value.trim()
},psw2:{
ele:form.psw2,
value:form.psw2.value.trim()
}
}
}
const isEmpty=(user)=>{
switch(true){
case user.uname.value.length ==0:
alert('用户名不能为空');
user.uname.ele.focus();
return false;
case user.email.value.length ==0:
alert('邮箱不能为空');
user.email.ele.focus();
return false;
case user.psw1.value.length ==0:
alert('用户名不能为空');
user.psw1.ele.focus();
return false;
case user.psw2.value.length ==0:
alert('用户名不能为空');
user.psw2.ele.focus();
return false;
default:
return true;
}
const isPswEqu = (user)=>{
if(user.psw1.value !== user.psw2.value){
alert('两次密码输入不一致');
user.psw1.ele.focus();
return false;
}else{
return true;
}
}
}
const createData = ()=>{
return{
uname:user.uname.value,
email:user.email.value,
password:user.psw1.value,
}
}
async function insertUser(data){
const response = await fetch('./lib/userHandle.php?action=register', {
method:'POST',
headers:{
'content-type':'application/json;charset=utf-8'
},
body:JSON.stringify(data)
});
const result =response.json();
if(result){
alert('注册成功');
location.href = 'index.php'
}else{
alert('注册失败');
location.href='register.php';
btn.form.uname.focus();
}
}
多行字符串,heredoc格式<<<ABC aaa ABC;可转译换行符\n等;nowdoc格式<<<ABC ‘aaa’ ABC;不转译;
if(!in_array($action, $allowOpts)){
echo <<<ABC
<script>
alert('非法访问‘);
location.href='/../login.php';
</script>
ABC;
}
// ''不能转译,""可以转译换行符\n等
// 定界符heredoc,解析变量
$str = <<<POEM
// 定界符nowdoc,不解析变量
<<< 'POEM'
POEM;
建议大家userHandle还没写好,最好不要把路径变过去,如果调试到半路停下了,登录、注销也用不了了!