学习总结
- session和cookie都可以保存用户的数据
- cookie是把用户数据保存在客户端的浏览器上
- session是把用户数据保存在服务器端的某个文件中
- cookie如果不设置路径,默认只能同文件夹下的文件可以访问它的cookie值,如果想让整站都访问必须设置路径
- session只要开启,就可以访问其中保存的值
1.首页index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/index.css">
<title>首页</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="header col-12">
<div><a href=""><img src="images/logo.png" alt=""></a></div>
<div class="nav">
<div><a href="">首页</a></div>
<div><a href="">商品推销</a></div>
<div><a href="">商品展示</a></div>
</div>
<div class="slogin">
<?php
session_start();
if(isset($_SESSION['userNc'])):
?>
<div>欢迎:<span style="color: red"><?php echo $_SESSION['userNc']; ?></span></div>
<div><a href="session/handle.php?action=logout" target="main">退出</a></div>
<?php
else:
?>
<div><a href="session/login.php" target="main">session登录</a></div>
<div><a href="session/register.php" target="main">session注册</a></div>
<?php
endif;
?>
</div>
<div class="clogin">
<div><a href="cookie/login.php" target="main">cookie登录</a></div>
<div><a href="cookie/register.php" target="main">cookie注册</a></div>
</div>
</div>
</div>
<div class="row">
<div class="col-1"></div>
<div class="aside col-2">
<div><a href="select.php" target="main"><span>查询商品</span> </a></div>
<div><a href="insert.php" target="main"><span>添加商品</span> </a></div>
<div><a href="update.php" target="main"><span>修改商品</span> </a></div>
<div><a href="delete.php" target="main"><span>删除商品</span> </a></div>
</div>
<div class="main col-8">
<iframe src="select.php" frameborder="0" width="100%" height="100%" id="main" name="main"></iframe>
</div>
<div class="col-1"></div>
</div>
<div class="row">
<div class="footer col-12">
footer
</div>
</div>
</div>
</body>
</html>
1.1首页样式表index.css
@import "reset.css";
.container {
min-height: 600px;
margin-left: auto;
margin-right: auto;
display: grid;
gap: 5px;
}
/* 每行按12列的栅格布局 */
.container > .row {
display: grid;
grid-template-columns: repeat(12, 1fr);
gap: 5px;
}
/* 页眉 */
.container > .row > .header {
height: 60px;
background-color: darkturquoise;
display: flex;
flex-flow: row nowrap;
align-items: center;
}
.container > .row > .header div {
display: flex;
flex-flow: row nowrap;
align-items: center;
}
.container > .row > .header a {
height: 100%;
width: 100%;
padding: 0px 5px;
font-size: 1.2rem;
font-weight: bolder;
}
.container > .row > .header > .nav {
margin-left: 40px;
}
.container > .row > .header > .nav a {
padding: 0px 20px;
}
.container > .row > .header > .slogin {
margin-left: auto;
margin-right: 20px;
}
/* 左侧边栏 */
.container > .row > .aside {
background-color: #ccc;
}
/* 侧边栏 */
.container > .row > .aside {
min-height: 500px;
background-color: #ccc;
border-radius: 3px;
display: flex;
flex-flow: column nowrap;
padding: 5px;
}
/* 侧边栏导航 */
.container > .row > .aside > div {
background-color: #58c4f2;
height: 40px;
margin: 2px 8px;
border-radius: 10px;
}
.container > .row > .aside > div > a {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.container > .row > .aside > div > a > span {
font-size: 1.2rem;
letter-spacing: 2px;
}
.container > .row > .aside > div:hover {
background-color: lightgreen;
box-shadow: 0 0 5px #555;
}
/* 网页主体部分 */
.container > .row > .main {
background-color: #ccc;
}
/* 除页眉页脚的中间部分 */
.container > .row:nth-of-type(2) {
min-height: 600px;
}
/* 页脚 */
.container > .row > .footer {
height: 165px;
background-color: darkturquoise;
}
.col-1 {
grid-column-end: span 1;
}
.col-2 {
grid-column-end: span 2;
}
.col-3 {
grid-column-end: span 3;
}
.col-4 {
grid-column-end: span 4;
}
.col-5 {
grid-column-end: span 5;
}
.col-6 {
grid-column-end: span 6;
}
.col-7 {
grid-column-end: span 7;
}
.col-8 {
grid-column-end: span 8;
}
.col-9 {
grid-column-end: span 9;
}
.col-10 {
grid-column-end: span 10;
}
.col-11 {
grid-column-end: span 11;
}
.col-12 {
grid-column-end: span 12;
}
- 首页效果展示
2.登录页面 login.php
<link rel="stylesheet" href="../css/form.css">
<div class="log">
<form action="handle.php?action=login" method="POST">
<h2>用户登录session</h2>
<div>
<label for="userNmae">邮箱:</label>
<input type="email" name="userName" id="userName" placeholder="请输入邮箱" require autofocus>
</div>
<div>
<label for="passWord">密码:</label>
<input type="password" name="passWord" id="passWord" placeholder="请输入密码" require>
</div>
<div>
<button type="submit">登录</button>
</div>
<div><a href="register.php">还没有账号,注册一个吧!</a></div>
</form>
</div>
- 登录页面
3.注册页面 register.php
<link rel="stylesheet" href="../css/form.css">
<div class="reg">
<form action="handle.php?action=regist" method="POST" onsubmit="return checkform(form)">
<h2>用户注册cookie</h2>
<div>
<label for="userNmae">注册邮箱:</label>
<input type="email" name="userName" id="userName" placeholder="angle@email.com" require autofocus>
</div>
<div>
<label for="userNc">注册昵称:</label>
<input type="text" name="userNc" id="userNc" placeholder="请输入昵称">
</div>
<div>
<label for="pwd1">注册密码:</label>
<input type="password" name="pwd1" id="pwd1" placeholder="请输入密码" require>
</div>
<div>
<label for="pwd2">确认密码:</label>
<input type="password" name="pwd2" id="pwd2" placeholder="请输入确认密码" require>
</div>
<div>
<button type="submit">注册</button>
</div>
<div>
<a href="login.php">我有账号,直接登录</a>
</div>
<script>
function checkform(form) {
//注册信息不能为空
for (i = 0; i < form.length; i++) {
if (form.elements[i].value.trim() == "") {
alert("请将注册信息填写完整!");
form.elements[i].focus();
return false;
}
}
// 两次密码必须相同
if (form.pwd1.value.trim() !== form.pwd2.value.trim()) {
alert("两次密码必须相同");
form.pwd2.focus();
return false;
}
}
</script>
</form>
</div>
- 注册页面
4.登录注册的样式表
@import "reset.css";
form {
margin-left: auto;
margin-right: auto;
margin-top: 20px;
background-color: darkcyan;
border-radius: 10px;
max-width: 500px;
min-height: 400px;
box-shadow: 0px 0px 2px #555;
display: flex;
flex-flow: column nowrap;
justify-content: space-evenly;
align-items: center;
}
form > h2 {
font-size: 2rem;
}
form label {
font-size: 1.5rem;
font-weight: bolder;
}
form input {
width: 150px;
height: 30px;
}
5.登录和注册的处理页面
<?php
session_start();//开启session
require '../autoLoad.php';
use compotents\conn\DBconn;
$user = new DBconn();
$table = 'tb_user';
$where = '*';
$data = [];
$action = $_GET['action'];
switch($action):
//处理注册操作
case 'regist':
if($_SERVER['REQUEST_METHOD']==='POST'):
$name = trim($_POST['userName']);
$nc = trim($_POST['userNc']);
$pwd = md5(trim($_POST['pwd1']));
$tpwd = trim($_POST['pwd1']);
$rdate = date('Y-m-d');
$data = ['name'=>"$name",'nc'=>"$nc",'password'=>"$pwd",'tpassword'=>"$tpwd",'regdate'=>"$rdate"];
//先判断一下用户名是否已经注册
$where = "`name`='$name'";
$res = $user->select($table,$where);
if(count($res)):
exit('<script>alert("邮箱已经注册过了,可直接登录");location.href="login.php";</script>');
else:
$rowCount = $user->insert($table,$data); //返回受影响的记录条数
if($rowCount):
exit('<script>alert("注册成功");location.href="login.php";</script>');
else:
exit('<script>alert("注册失败");location.href="register.php";</script>');
endif;
endif;
else:
die('请求类型错误!');
endif;
break;
//处理登录操作
case 'login':
if($_SERVER['REQUEST_METHOD']==='POST'):
$name = trim($_POST['userName']);
$pwd = md5(trim($_POST['passWord']));
$where = "`name`='$name' and `password`='$pwd'";
$res = $user->select($table,$where);
if(count($res)):
$nc = $res[0]['nc'];
$_SESSION['userNc'] = $nc;//写入session
exit('<script>alert("登录成功");location.href="../select.php";</script>');
else:
exit('<script>alert("登录失败,用户名或密码错误");location.href="login.php";</script>');
endif;
else:
die('请求类型错误!');
endif;
break;
//处理退出操作
case 'logout':
if(isset($_SESSION['userNc'])):
session_destroy();//清除session的值
exit('<script>alert("退出成功");location.href="login.php";</script>');
endif;
break;
default:
echo '不存在此操作';
endswitch;
?>
6.登录成功设置cookie
<?php
setcookie('userNc',$nc); //设置cookie
?>
- 未登录不允许进行添加,更新和删除操作,在文件头部进行session判断
<?php
session_start();
if(!(isset($_SESSION['userNc']))):
exit('<script>alert("您还未登录,请登录后操作");location.href="session/login.php";</script>');
endif;
?>
未登录
登录成功