主题:
分别使用cookie和session方式验证用户登录信息。
实现效果:
公共头部文件(header.php):
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title> <?php //设置当前的页面标题 echo isset($page_title) ? $page_title :'默认标题'; ?> </title> <link rel="stylesheet" href="css/reset.css"> <link rel="stylesheet" href="css/common.css"> <!-- <link rel="stylesheet" href="css/login.css"> --> </head> <body> <!-- 公共导航 --> <div class="nav"> <a href="">xxxxxxxxxxxxxx</a> <a href="">xxxxxxxxxxxxxx</a> <a href="">xxxxxxxxxxxxxx</a> <a href="">xxxxxxxxxxxxxx</a> <a href="">用户信息</a> </div>
运行实例 »
点击 "运行实例" 按钮查看在线实例
公共底部文件(footer.php):
<div class="footerbox"> <div class="footer clear"> <div class="footleft"> 填充内容 </div> <div class="footright"> 填充内容 </div> </div> <hr> <div class="footerbottom"> <p><a href="">ICP备xxxxxxxxx号</a> | 公网安备xxxxxxxxx</p> </div> </div> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
公共数据库连接文件(connect.php):
<?php //创建连接参数: 因为连接参数不会经常变化,所以推荐使用常量 define ('DB_HOST', 'localhost');//数据库主机名 define ('DB_USER', 'root');//数据库用户名 define ('DB_PASS', 'root');//数据库密码 define ('DB_NAME', 'php');//数据库名称 define ('DB_CHAR', 'utf8');//数据库字符编码 // 连接数据库 $dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); //连接失败一定会返回错误编号,可以根据编号判断,也可用 $db是否为false进行判断 if (mysqli_connect_errno($dbc)) { echo '连接失败'.mysqli_connect_error($dbc); } mysqli_select_db($dbc, DB_NAME); //选择要操作的数据库 mysqli_set_charset($dbc, DB_CHAR); //设置客户端默认字符编码集
运行实例 »
点击 "运行实例" 按钮查看在线实例
公共函数库文件(function.php):
<?php //登录公共函数库 /医院 * 用户自定义跳转地址 * @param string $page */ function redirect_user($page = 'index.php') { //默认url格式 $url = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']); //如果有,去掉url右侧的斜线 $url = rtrim($url, '/\\'); //添加上当前的脚本名称,默认为:index.php $url .= '/'.$page; //跳转到指定目标地址 header('Location:'. $url); //退出当前函数,这是一个好习惯,否则后面代码仍会执行,仅仅不会在当前页面输出罢了 exit(); } /医院 * 验证用户登录 * @param $dbc * @param string $email * @param string $password */ function check_login($dbc, $email='', $password='') { //初始化错误信息数组 $errors = []; //验证邮箱 if (empty($email)) { $errors[] = '邮箱地址不能为空'; } else { //mysqli_real_escape_string():转义字符串的特殊字符 $e = mysqli_real_escape_string($dbc, trim($email)); } //验证密码 if (empty($password)) { $errors[] = '密码不能为空'; } else { $p = mysqli_real_escape_string($dbc, trim($password)); } //非空验证通过,即$error数组为空 if (empty($errors)) { //根据邮箱与密码来查询用户id与用户名 // 注意这里的数据库查询语句中的``不是'' $sql = "SELECT `user_id`,`user_name` FROM `user` WHERE `email`='$e' AND `password`=sha1('$p') "; //执行查询 $res = mysqli_query($dbc, $sql); //查询成功应该返回唯一一条记录 if (mysqli_num_rows($res) == 1) { //将查询结果解析到数组中 $row = mysqli_fetch_array($res, MYSQLI_ASSOC); //返回查询结果 // print_r($row);exit(); return [true, $row]; } else { //查询失败 $errors[] = '邮箱或密码不正确,请重新输入'; } } return [false, $errors]; }
运行实例 »
点击 "运行实例" 按钮查看在线实例
COOKIE方式:
主页(index.php):
<?php // 设置文档字符编码 header("Content-type: text/html; charset=utf-8"); // 设置标题 $page_title = '首页'; // 加载头部文件 include('inc/header.php'); // 登陆检测 if ((isset($_COOKIE['user_id'])) && basename($_SERVER['PHP_SELF']) != 'logout.php') { echo '<a href="logout.php">退出</a>'; } else { echo '<a href="login.php">登陆</a>'; } // 加载底部文件 include('inc/footer.php');
运行实例 »
点击 "运行实例" 按钮查看在线实例
登陆页面(login_page.php):
<?php /*用户登录页面*/ // 设置文档字符编码 header("Content-type: text/html; charset=utf-8"); // 设置标题 $page_title = '登陆页面'; // 加载头部文件 include('inc/header.php'); // 打印错误信息 ?> <form action="login.php" method="post"> <caption>用户登录</caption> <p> <label for="email">邮箱:</label> <input type="text" id="email" name="email"> </p> <p> <label for="password">密码:</label> <input type="password" id="password" name="password"> </p> <p> <button type="submit" id="submit" name="submit">登陆</button> </p> </form> <?php // 加载底部文件 include('inc/footer.php'); ?>
运行实例 »
点击 "运行实例" 按钮查看在线实例
验证登录信息(login.php):
<?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { // 加载公共函数库 require('inc/function.php'); // 加载连接数据库文件 require('db/connect.php'); // 验证登陆 list($check, $data) = check_login($dbc, $_POST['email'], $_POST['password']); // 验证通过 if ($check) { // 设置cookie setcookie('user_id', $data['user_id']); setcookie('user_name', $data['user_name']); // 跳转页面 redirect_user('loggedin.php'); } else { $errors = $data; } // 关闭连接 mysqli_close($dbc); } // 跳转到表单 include('login_page.php');
运行实例 »
点击 "运行实例" 按钮查看在线实例
登录成功页面(loggedin.php):
<?php // 设置文档字符编码 header("Content-type: text/html; charset=utf-8"); // 用户没有登录的验证 if (!isset($_COOKIE['user_id'])) { // 加载函数库 require('inc/function.php'); // 不跳转 redirect_user(); } // 设置标题 $page_title = '已登录'; // 加载头部文件 include('inc/header.php'); // 欢迎信息 echo <<< "WELCOME" <h2 style="color:red">登录成功</h2> <p>欢迎您:{$_COOKIE['user_name']}</p> <p><a href="logout.php">退出</a></p> WELCOME; // 加载底部文件 include('inc/footer.php');
运行实例 »
点击 "运行实例" 按钮查看在线实例
退出登录页面(logout.php):
<?php // 用户没有登录的验证 if (!isset($_COOKIE['user_id'])) { // 加载函数库 require('inc/function.php'); // 不跳转 redirect_user(); } else { setcookie('user_id', '', time()-3600); setcookie('user_name', '', time()-3600); } // 设置标题 $page_title = '未登录'; // 加载头部文件 include('inc/header.php'); // 欢迎信息 echo <<< "WELCOME" <h2 style="color:red">退出成功</h2> <p><a href="login.php">登陆</a></p> WELCOME; // 加载底部文件 include('inc/footer.php');
运行实例 »
点击 "运行实例" 按钮查看在线实例
session方式:将index.php,login.php,loggedin.php,logout.php中的cookie方式修改为session方式即可。
总结:
两种验证方式比较相似,注意session方式每个服务器后台文件都需要开启会话才能生效。