cookie:
index.php:
实例
<?php $page_title = '首页'; //导入头部 include('inc/header.php'); echo '<h2 style="color:red">我是首页</h2>'; //登录检测 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.php:
实例
<?php if($_SERVER['REQUEST_METHOD'] == 'POST') { //验证用户的邮箱和密码 //跳转到指定的页面 //加载公共函数库 require ('inc/function.php'); //连接数据库 require ('inc/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 //没有登录的验证 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');
运行实例 »
点击 "运行实例" 按钮查看在线实例
login_page.php:
实例
<?php /** * 用户登录页面 * 1.输出错误信息 * 2.输出登录表单 */ $page_title = '用户登录'; //导入头部 include('inc/header.php'); //打印错误信息 ?> <h2 style="color:red">用户登录</h2> <form action="login.php" method="post"> <p> <label for="email">邮箱:</label> <input type="email" name="email" id="email" value=""> </p> <p> <label for="password">密码:</label> <input type="password" name="password" id="password" value=""> </p> <p> <button type="submit" name="submit" id="submit">登录</button> </p> </form> <?php //导入底部 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');
运行实例 »
点击 "运行实例" 按钮查看在线实例
inc/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> </head> <body> <div style="text-align: center"> <div style="background-color: skyblue; height: 30px;"></div>
运行实例 »
点击 "运行实例" 按钮查看在线实例
inc/footer.php:
实例
<div style="background-color: green; height: 30px;"> </div> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
inc/function.php:
实例
<?php /** * 用户自定义跳转地址,不设定就往首页跳 * @param string $page */ function redirect_user($page='index.php') { //默认地址 //当前主机名 //当前脚本名称 $url = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']); //去掉右侧的正反斜线 $url = rtrim($url, '/\\'); //生成自定义跳转地址 $url .= '/'.$page; //跳转到指定的目标地址,header(),头部设定 header('Location:'. $url); //退出当前的函数或脚本 exit(); } /** * @param $dbc * @param string $email * @param string $password * @return array */ function check_login($dbc, $email='', $password='') { //创建错误信息数组 $errors = []; //验证邮箱 if (empty($email)) { $errors[] = '邮箱不能为空'; } else { $e = mysqli_real_escape_string($dbc, trim($email)); } //验证密码 if (empty($password)) { $errors[] = '密码不能为空'; } else { $p = mysqli_real_escape_string($dbc, trim($password)); } if (empty($errors)) { //密码加密 $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); // echo '<pre>'; // print_r($row); die(); return [true, $row]; } else { $errors[] = '邮箱或密码不正确,请重新输入'; } } return [false, $errors]; }
运行实例 »
点击 "运行实例" 按钮查看在线实例
inc/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); //连接失败提示 if (mysqli_connect_errno($dbc)) { echo '连接失败'.mysqli_connect_error(); } //选择数据库名称 mysqli_select_db($dbc, DB_NAME); mysqli_set_charset($dbc, DB_CHAR);
运行实例 »
点击 "运行实例" 按钮查看在线实例
效果图:
session:
index.php:
实例
<?php //向浏览器生成一个cookie = PHPSESSID session_start(); $page_title = '首页'; //导入头部 include('inc/header.php'); echo '<h2 style="color:red">我是首页</h2>'; //登录检测 if ((isset($_SESSION['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.php:
实例
<?php session_start(); if($_SERVER['REQUEST_METHOD'] == 'POST') { //验证用户的邮箱和密码 //跳转到指定的页面 //加载公共函数库 require ('inc/function.php'); //连接数据库 require ('inc/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']); $_SESSION['user_id'] = $data['user_id']; $_SESSION['user_name'] = $data['user_name']; //跳转 redirect_user('loggedin.php'); } else { $errors = $data; } //关闭数据库连接 mysqli_close($dbc); } //加载 include ('login_page.php');
运行实例 »
点击 "运行实例" 按钮查看在线实例
loggedin.php:
实例
<?php session_start(); //没有登录的验证 if (!isset($_SESSION['user_id'])) { require ('inc/function.php'); redirect_user(); } //已经登录 $page_title = '已经登录'; //导入头部 include('inc/header.php'); //打印欢迎信息 echo <<< "WELCOME" <h2 style="color:red">登录成功</h2> <p>欢迎您:{$_SESSION['user_name']}</p> <p><a href="logout.php">退出</a></p> WELCOME; //导入底部 include('inc/footer.php');
运行实例 »
点击 "运行实例" 按钮查看在线实例
login_page.php:
实例
<?php /** * 用户登录页面 * 1.输出错误信息 * 2.输出登录表单 */ $page_title = '用户登录'; //导入头部 include('inc/header.php'); //打印错误信息 ?> <h2 style="color:red">用户登录</h2> <form action="login.php" method="post"> <p> <label for="email">邮箱:</label> <input type="email" name="email" id="email" value=""> </p> <p> <label for="password">密码:</label> <input type="password" name="password" id="password" value=""> </p> <p> <button type="submit" name="submit" id="submit">登录</button> </p> </form> <?php //导入底部 include('inc/footer.php'); ?>
运行实例 »
点击 "运行实例" 按钮查看在线实例
logout.php:
实例
<?php session_start(); //没有登录的验证 if (!isset($_SESSION['user_id'])) { require ('inc/function.php'); redirect_user(); } else { // setcookie('user_id', '', time()-3600); // setcookie('user_name', '', time()-3600); $_SESSION = []; session_destroy(); setcookie('PHPSESSID', '', 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');
运行实例 »
点击 "运行实例" 按钮查看在线实例
总结:
cookie:
设置cookie
setcookie('username', 'peter Zhu', time()+3600); //time()是指1970到现在的时间,秒数
2.查看:$_COOKIE
echo '用户名: ', $_COOKIE['username'], '<br>';
echo '邮箱: ', $_COOKIE['email'], '<br>';
3.更新
setcookie('username', '朱老师');
4.删除
方法1,只传变量名
setcookie('username');
方法2,传入空值
setcookie('username', '');
方法3,传入一个过期时间
setcookie('username', '',time()-3600);
4.物理删除cookie: unset($_COOKIE[name]),彻底干掉了cookie
unset($_COOKIE['username']);
session与cookie类似,用session前要有session_start();
session清空方式:
1.$_SESSION = []; 设为空
2.session_destroy(); 删除目录
3.setcookie('PHPSESSID', '', time()-3600); 删除cookie中的PHPSESSID