
批改狀態(tài):合格
老師批語:
首先在主頁表頭添加點擊事件,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);
表單驗證主要有獲取數(shù)據(jù),非空驗證,兩次密碼相同驗證,插入數(shù)據(jù)等幾步,可分別用函數(shù)驗證;數(shù)據(jù)插入部分尚未完成;
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;可轉(zhuǎn)譯換行符\n等;nowdoc格式<<<ABC ‘a(chǎn)aa’ ABC;不轉(zhuǎn)譯;
if(!in_array($action, $allowOpts)){
echo <<<ABC
<script>
alert('非法訪問‘);
location.href='/../login.php';
</script>
ABC;
}
// ''不能轉(zhuǎn)譯,""可以轉(zhuǎn)譯換行符\n等
// 定界符heredoc,解析變量
$str = <<<POEM
// 定界符nowdoc,不解析變量
<<< 'POEM'
POEM;
建議大家userHandle還沒寫好,最好不要把路徑變過去,如果調(diào)試到半路停下了,登錄、注銷也用不了了!
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號