小实战:注册登录功能
一、index.php
二、register.php
三、userHandle.php
四、相关截图
五、疑问:能否获取了users.php文件数据,然后注册的时候重写进去呢,然后就可以直接用新注册的用户数组进行登录呢。" class="reference-link">
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<?php
$arr = require 'data/data.php';
?>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="keywords" content="<?= $arr[0] ?>" />
<meta name="description" content="<?= $arr[1] ?>" />
<link rel="stylesheet" href="static/css/style.css" />
<title><?= $arr[2] ?></title>
</head>
<body>
<?php
// 引入配置文件
include __DIR__. '/config/common.php';
// 引入头部 -->
include TMPL_PATH_PUBLIC. '/header.php';
// 引入新闻数据 -->
$news = include DATA_PATH . '/news.php';
// 引入产品数据 -->
$items = include DATA_PATH . '/items.php';
$users = include DATA_PATH . '/users.php';
?>
<!-- 主体 -->
<main>
<!-- 新闻列表 -->
<div class="news">
<h3>新闻列表</h3>
<div class="list">
<? foreach ($news as $k => $new) :
extract($new);
if ($k<5) :
?>
<a href="<?= $url ?>" target=_blank>
<?= mb_substr($title,0,35) ?>...
</a>
<? endif ?>
<? endforeach ?>
</div>
</div>
<!-- 产品列表 -->
<div class="items">
<h3>产品列表</h3>
<div class="list">
<? foreach ($items as $k => $item) :
extract($item);
if ($k<4) :
?>
<div class="item">
<img src="<?= $img ?>" alt="<?= $title ?>" />
<a href="<?= $url ?>" target=_blank>
<?= mb_substr($title,0,9) ?>...
</a>
</div>
<? endif ?>
<? endforeach ?>
</div>
</div>
</main>
<!-- 引入页脚 -->
<?php include 'template/public/footer.php' ?>
</body>
</html>
二、register.php
<!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" />
<meta name="keywords" content="新闻,产品,合肥" />
<meta name="description" content="专业生产中高档办公家具,行业畅销20年" />
<link rel="stylesheet" href="static/css/style.css" />
<title>注册</title>
</head>
<body>
<?php
// 引入配置文件
include __DIR__. '/config/common.php';
// 引入头部 -->
include TMPL_PATH_PUBLIC. '/header.php';
?>
<!-- 主体 -->
<main>
<form class="login" id="register" method="post" action="">
<table>
<caption>用户注册</caption>
<tbody>
<tr>
<td><label for="uname">昵称:</label></td>
<td><input type="text" name="uname" id="uname" placeholder="uname" required autofocus /></td>
</tr>
<tr>
<td><label for="email">邮箱:</label></td>
<td><input type="email" name="email" id="email" placeholder="username@email.com" required /></td>
</tr>
<tr>
<td><label for="password">密码:</label></td>
<td><input type="password" name="password" id="password" placeholder="******" required /></td>
</tr>
<tr>
<td><label for="rePassword">确认:</label></td>
<td><input type="password" name="rePassword" id="rePassword" placeholder="******" required /></td>
</tr>
<tr>
<td colspan="2"><button type="button" onclick="addUser(this)">提交</button></td>
</tr>
</tbody>
</table>
</form>
<p>
<a href="login.html">已有帐号,请直接登录</a>
</p>
</main>
<!-- 引入页脚 -->
<?php include 'template/public/footer.php' ?>
</body>
</html>
<script>
function addUser(btn) {
// 1. 获取用户的输入
const user = getInput(btn.form);
// 2. 非空验证
if (isEmpty(user)) {
// 3. 验证二次密码是否相等?
if (isPswEqu(user)) {
// 4. 创建提交的数据JSON
const data = createData(user);
// 5. 异步提交
insertUser(data);
}
}
}
// 1. 获取用户输入
const getInput = (form) => {
return {
// 呢称:
uname: {
// dom元素
ele: form.uname,
// 值
value: form.uname.value.trim()
},
// 邮箱:
email: {
ele: form.email,
value: form.email.value.trim()
},
// 密码:
password: {
ele: form.password,
value: form.password.value.trim()
},
// 确认密码:
rePassword: {
ele: form.rePassword,
value: form.rePassword.value.trim()
},
}
}
// 2. 非空验证
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.password.value.length === 0:
alert('密码不能为空');
user.password.ele.focus();
return false;
case user.rePassword.value.length === 0:
alert('重复密码不能为空');
user.rePassword.ele.focus();
return false;
default:
return true;
}
}
// 3. 验证二次密码是否相等?
const isPswEqu = (user) => {
if (user.password.value !== user.rePassword.value) {
alert('二次密码不相等,重新输入');
user.password.ele.focus();
return false;
} else {
return true;
}
}
// 4. 创建提交的数据JSON
const createData = (user) => {
return {
uname: user.uname.value,
email: user.email.value,
password: user.password.value,
}
}
// 5. 异步提交
async function insertUser(data) {
console.log(data);
const url = './lib/userHandle.php?action=register';
const response = await fetch(url, {
method: 'POST',
headers: {
'content-type': 'application/json;charset=utf-8'
},
body: JSON.stringify(data)
});
const result = await response.json();
console.log(result);
// 响应处理
if (result) {
alert('添加成功');
// 跳到首页
location.href = 'index.php';
} else {
alert('添加失败');
location.href = 'register.php';
btn.form.email.focus();
}
}
</script>
三、userHandle.php
<?php
session_start();
require __DIR__ . '/../config/common.php';
$users = require DATA_PATH . '/users.php';
// 获取用户的操作动作
$action = strtolower($_GET['action']);
$prompt = false;
switch ($action):
case 'login':
// 1. 获取用户登录数据,不能用传统的表单格式提交
// json当成文本流原始数据接收
$json = file_get_contents('php://input');
// 2. $json 并非php能识别的数据类型,它只是json格式的字符串而已
// json -> php.array, true: 数组
$user = json_decode($json, true);
// 3. 解析用户提交的数据,做预处理判断:
$email = $user['email'];
$salt = 'wwwphpcn';
$password = md5($user['password'] . $salt);
// 4. 用户过滤
$res = array_filter($users,function($item) use ($email, $password) {
return $item['email'] === $email && $item['password'] === $password;
});
$res = array_values($res);
// 5. 结果分析
// 默认检验用户信息 登录不通过
if (count($res) === 1) {
$prompt = true;
$_SESSION['user']['email'] = $res[0]['email'];
$_SESSION['user']['name'] = $res[0]['name'];
}
break;
case 'logout':
if(session_destroy()){
$prompt = true;
};
break;
case 'register':
$oriCount = count($users);
$json = file_get_contents('php://input');
$user = json_decode($json, true);
$user['password'] = md5($user['password']);
$user['id'] = count($users) + 1;
$users[] = $user;
if (count($users) === $oriCount + 1) {
$prompt = true;
}
break;
endswitch;
echo json_encode($prompt);
四、相关截图
五、疑问:能否获取了users.php文件数据,然后注册的时候重写进去呢,然后就可以直接用新注册的用户数组进行登录呢。" class="reference-link">
能否获取了users.php文件数据,然后注册的时候重写进去呢,然后就可以直接用新注册的用户数组进行登录呢。
【文章出处:香港站群多ip服务器 http://www.558idc.com/hkzq.html提供,感恩】