
批改狀態(tài):合格
老師批語:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>用戶注冊(cè)表單</title>
</head>
<body>
<form action="handle.php?action=register" method="POST">
<fieldset style="display: inline-block;background:lightcyan">
<legend align="center">用戶注冊(cè)</legend>
<p>
<label for="name">用戶名:</label>
<!-- require :規(guī)定必需在提交之前填寫輸入字段 -->
<input type="txet" name="name" id="name" placeholder="請(qǐng)輸入用戶名" require>
</p>
<p>
<label for="password">郵箱:</label>
<input type="email" name="email" id="email" placeholder="請(qǐng)輸入郵箱" require>
</p>
<p>
<label for="password">密碼:</label>
<input type="password" name="password" id="pw" placeholder="請(qǐng)輸入密碼" require>
</p>
<p>
<label for="password">確認(rèn)密碼:</label>
<input type="password" name="repassword" id="repw" placeholder="請(qǐng)?jiān)俅屋斎朊艽a" require>
</p>
<p>
<button>提交</button>
</p>
</fieldset>
</form>
<script>
document.querySelector('button').addEventListener('click',function(event) {
$name = document.querySelector('#name').value;
$pwd = document.querySelector('#pw').value;
$repwd = document.querySelector('#repw').value;
console.log($name);
console.log($pwd);
console.log($repwd);
if ($name === '') {
event.preventDefault();
alert("用戶名不能為空");
return false;
}
if ($pwd === '') {
event.preventDefault();
alert("密碼不能為空");
return false;
}
if ($repwd === '') {
event.preventDefault();
alert("請(qǐng)輸入確認(rèn)密碼");
return false;
}
if ($name != '' && $pwd != $repwd) {
event.preventDefault();
alert("兩次輸入的密碼不一致");
}
})
</script>
</body>
</html>
<?php
session_start();
// 判斷用戶是否已經(jīng)登錄?
if(isset($_SESSION['user'])) $user = unserialize($_SESSION['user']);
// print_r(user);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<nav>
<?php if (isset($user)): ?>
<a href="" id="logout">退出</a>
<?php else : ?>
<a href="login.php">登錄</a>
<?php endif ?>
</nav>
<script>
let s = document.getElementById('logout');
if (s) {
document.querySelector('#logout').addEventListener('click', function(event) {
if (confirm('是否退出?')){
// 禁用默認(rèn)跳轉(zhuǎn)行為
event.preventDefault();
// 跳轉(zhuǎn)到處理器
location.assign('handle.php?action=logout');
}
});
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<?php
// 判斷用戶是否已經(jīng)登錄?
if (isset($_SESSION['user']))
echo '<script>alert("不要重復(fù)登錄");location.href="index.php"</script>';
?>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>用戶登錄表單</title>
</head>
<body>
<form action="handle.php?action=login" method="POST">
<fieldset style="display:inline-block; background:lightcyan">
<legend align="center">用戶登錄</legend>
<p>
<label for="name">用戶名:</label>
<input type="text" name="name" id="name" placeholder="請(qǐng)輸入用戶名" require>
</p>
<p>
<label for="email">郵箱:</label>
<input type="email" name="email" id="email" placeholder="user@email.com" require>
</p>
<p>
<label for="password">密碼:</label>
<input type="password" name="password" placeholder="不少于6位" require>
</p>
<p>
<button>提交</button>
</p>
</fieldset>
<br>
<a href="register.php">如果沒有帳號(hào),請(qǐng)先注冊(cè)</a>
</form>
</body>
</html>
<?php
//開啟會(huì)話
session_start();
// 根據(jù)用戶的不同請(qǐng)求,執(zhí)行不同的操作
// 比如:登錄,注冊(cè),退出
// 連接數(shù)據(jù)并獲取用戶表中的數(shù)據(jù)
$db = new PDO ('mysql:dbname=phpedu','root','root');
$stmt = $db->prepare('SELECT * FROM `user`');
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
// print_r($users);
// * 查看是否跳轉(zhuǎn)頁面并獲取index里的表單action
$action = $_GET['action'];
// * strtolower() 將字符串轉(zhuǎn)化為小寫
switch (strtolower($action)) {
// 登錄
case 'login':
// 要保證數(shù)據(jù)是通過POST請(qǐng)求發(fā)送的
// * REQUEST_METHOD:訪問頁面使用的請(qǐng)求方法($_SERVER:超全局變量)
// echo $_SERVER['REQUEST_METHOD'];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// var_dump($_SERVER['REQUEST_METHOD']);
// 先拿到登錄數(shù)據(jù)
// print_r($_POST)
// * extract() - 從數(shù)組中將變量導(dǎo)入到當(dāng)前的符號(hào)表
extract($_POST);
// $email = $_POST['email];
// $password = sha1($_POST['password']);
// * array_filter()- 使用回調(diào)函數(shù)過濾掉數(shù)組的元素
// $result 是數(shù)組
$result = array_filter($users,function($users) use ($email,$password) {
return $users['name'] === $name && $users['password'] === md5($password);
});
if (count($result) === 1) {
// 驗(yàn)證成功,將用戶信息寫到SESSION
// print_r(serialize(array_pop($result)));
// $a = serialize(array_pop($result));
// print_r(unserialize($a));
// 將用戶信息序列化之后保存到SESSION中
$_SESSION['user'] = serialize(array_pop($result));
exit('<script>alert("驗(yàn)證通過");location.href="index.php"</script>');
} else {
exit('<script>alert("郵箱或密碼錯(cuò)誤");location.href="index.php"</script>');
}
} else {
// var_dump($_SERVER['REQUEST_METHOD']);
die('請(qǐng)求錯(cuò)誤');
}
break;
// 退出
case 'logout':
if (isset($_SESSION['user'])) {
session_destroy();
// * location.assign:載入一個(gè)新文檔,瀏覽器的后退按鈕可用
exit('<script>alert("退出成功");location.assign("index.php")</script>');
}
break;
// 注冊(cè)
case 'register':
// 1. 獲取到新用戶的信息
$name = $_POST['name'];
$email = $_POST['email'];
$password = md5($_POST['password']);
$register_time = time();
// 2. 將新用戶添加到表中
$sql = 'INSERT `user` SET `name`=?,`email`=?,`password`=?,`register_time`=?';
$stmt = $db->prepare($sql);
$stmt->execute([$name,$email,$password,$register_time]);
if ($stmt->rowCount() === 1){
exit ('<script>alert("注冊(cè)成功");location.href="index.php"</script>');
} else {
exit('<script>alert("注冊(cè)失敗");location.href="index.php"</script>');
}
break;
}
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)