SESSION 登录实战
第一步:数据库增加数据
CREATE TABLE `user` ( `uid` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID', `phone` varchar(20) NOT NULL DEFAULT '' COMMENT '手机号', `name` varchar(50) NOT NULL DEFAULT '' COMMENT '姓名', `pwd` varchar(32) NOT NULL DEFAULT '' COMMENT '密码', `age` int(3) unsigned NOT NULL DEFAULT '18' COMMENT '年龄', `sex` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '性别,值为1时是男性,值为2时是女性,值为0时是未知', `status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '1为正常,0为禁止', `last_time` int(11) unsigned DEFAULT '0' COMMENT '最后登录时间', PRIMARY KEY (`uid`) USING BTREE ) ENGINE=MyISAM AUTO_INCREMENT=17 DEFAULT CHARSET=utf8 COMMENT='用户--用户信息表'; INSERT INTO `user` VALUES (1, '18011112222', '欧阳克', 'e10adc3949ba59abbe56e057f20f883e', 18, 1, 1, 1561125595);
第二步:pdo连接数据库(pdo.php)
<?php $db = [ 'type' => 'mysql', 'host' => 'localhost', 'dbname' => 'shy', 'username' => 'root', 'password' => 'root' ]; $dsn = "{$db['type']}:host={$db['host']};dbname={$db['dbname']}"; $username = $db['username']; $password = $db['password']; try { $pdo = new PDO($dsn, $username, $password); print_r($pdo); } catch (PDOException $e) { die('连接失败' . $e->getMessage()); }
第三步:设置派发器(dispatch.php)
<?php // 请求派发器: 前端控制器 // 功能就是获取到用户的请求, 并调用不同的脚本进行处理和响应 session_start(); // 连接数据库 require 'pdo.php'; // 获取请求参数 $action = isset($_GET['action']) ? $_GET['action'] : 'login'; $action = htmlentities(strtolower(trim($action))); // 请求分发 switch ($action) { // 1. 登录页面 case 'login': // 加载登录表单 include 'login.php'; break; // 2. 验证登录 case 'check': include 'check.php'; break; // 3. 退出登录 case 'logout': include 'logout.php'; break; // 默认操作 default: include 'index.php'; break; }
第四步:设置首页、登录页面、验证页面及登出页面
首页index.php
<?php session_start(); if (isset($_SESSION['name'])){ echo '用户: ' . $_SESSION['name'] . '已登录<br>'; echo '<a href="dispatch.php?action=logout">退出</a>'; } else { // 2. 未登录,就跳转到登录页面 echo '<a href="dispatch.php?action=login">请登录</a>'; }
登录login.php
<?php ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用户登录</title> </head> <body> <h3>用户登录</h3> <form action="dispatch.php?action=check" method="post"> <p> <label for="phone">手机号:</label> <input type="phone" name="phone" id="phone"> </p> <p> <label for="password">密码:</label> <input type="password" name="password" id="password"> </p> <p> <button>提交</button> </p> </form> </body> </html>
验证check.php
<?php if (!empty($_POST['phone']) && !empty($_POST['password'])){ $phone = $_POST['phone']; $pwd = md5($_POST['password']); $sql = 'SELECT * FROM `user` WHERE `phone`=:phone AND `pwd`=:pwd'; $stmt = $pdo->prepare($sql); $stmt->execute(['phone'=>$phone,'pwd'=>$pwd]); $user = $stmt ->fetch(PDO::FETCH_ASSOC); // print_r($user); if (empty($user)){ echo '未找到用户'; }else{ $_SESSION['name']=$user['name']; echo '<script>alert("登录成功");location.assign("index.php");</script>'; exit; } }else{ echo '未提交'; }
登出logout.php
<?php session_destroy(); echo '<script>alert("退出成功");location.assign("index.php");</script>';
练熟pdo操作,增删查改(手写)
总结:
1.session有cookie做基础,安全性较cookie高一些
2.在使用session前需要设置session_start();
3.会话控制利用派发器较方便