PHP基础9
主要知识点
1). cookie 基础运用
2). session 基础运用
cookie与session会话机制的优缺点分析
代码
Cookie
inc/config.php
<?php DEFINE('DB_HOST','127.0.0.1'); DEFINE('DB_USER','root'); DEFINE('DB_PASS','root'); DEFINE('DB_NAME','php'); DEFINE('DB_CHAR','utf8');
inc/db.php
<?php require 'config.php'; $db = new mysqli(DB_HOST,DB_USER,DB_PASS, DB_NAME); if($db->connect_errno){ die('连接错误'.$db->connect_errno.': '. $db->connect_error); } $db->set_charset(DB_CHAR);
inc/function.php
<?php // 成功后跳转 function redirect_user($page='demo2.php') { // 默认值 $url = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']); // 如果有,去掉url 右侧的斜线 $url = rtrim($url, '/\\'); // 添加脚本名称 $url .= '/'.$page; // 跳转指定地址 header('Location:'.$url); exit(); } function check_login($db,$name='',$password=''){ // 初始化,保存错误信息 $errors = []; // 非空验证 if(empty($name)){ $errors[] = '名称不能为空'; } else { $n = mysqli_real_escape_string($db,trim($name)); // $n = $name; } if(empty($password)){ $errors[] = '密码不能为空'; } else { $p = mysqli_real_escape_string($db,trim($password)); // $p = $password; } // 到表验证 if(empty($errors)) { $sql = "SELECT `user_id`,`email` FROM `user` WHERE `user_name` = '$n' AND `password` = sha1('$p')"; // die($sql); $res = mysqli_query($db,$sql); // var_dump($res);die; if( mysqli_num_rows($res) == 1 ){ $row = mysqli_fetch_array($res,MYSQLI_ASSOC); return [true,$row]; } else { $errors[] = '用户名或者密码错误'; } return [false,$errors]; } }
inc/header.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title> <?php echo isset($page_title) ? $page_title : '默认'; ?> </title> </head> <body> <h3>我是public</h3>
inc/footer.php
<h5>我是底部</h5> </body> </html>
demo2.php
<?php // 首页 $page_title = '首页'; // 头部 include 'inc/header.php'; if(isset($_COOKIE['user_id']) && basename($_SERVER['PHP_SELF']) !='login.php'){ echo '<a href="loginout.php">退出</a><br>'; } else{ echo '<a href="islogin.php">登录</a><br>'; } // 底部 include 'inc/footer.php';
home.php
<?php // 登录成功 if(!isset($_COOKIE['user_id'])){ require './inc/function.php'; redirect_user(); } $page_title = 'home'; include 'inc/header.php'; echo <<< HOME <h3>welcome</h3> <p>user_id:{$_COOKIE['user_id']}</p> <p>email:{$_COOKIE['email']}</p> <p><a href='loginout.php'>退出</a></p> HOME; include 'inc/footer.php';
isLogin.php
<?php // 登录页 // require './inc/db.php'; // require './inc/function.php'; // 请求判断 if($_SERVER['REQUEST_METHOD'] == 'POST'){ require './inc/db.php'; require './inc/function.php'; list($check,$data) = check_login($db,$_POST['name'],$_POST['password']); // check_login($db,'admin','123456'); if($check){ // 设置cookie setcookie('user_id',$data['user_id']); setcookie('email',$data['email']); // 跳转 redirect_user('home.php'); } else { $errors = $data; } // 关闭数据库 $db = null; } include 'login.php';
login.php
<?php // 登录模板 $page_title = 'login'; include 'inc/header.php'; // 错误信息 if(isset($errors) && !empty($errors)){ $error_msg = '<div>'; foreach($errors as $msg) { $error_msg .= $msg . '<br>'; } echo $error_msg."</div>"; } ?> <h4>用户登录</h4> <form action="islogin.php" method='post'> <p> <label for="name">姓名</label> <input type="text" name='name' id='name' value="<?php echo isset($_POST['name']) ? $_POST['name'] : ''; ?>" > </p> <p> <label for="password">手机号</label> <input type="password" name='password' id='password' value="<?php echo isset($_POST['password']) ? $_POST['password'] : ''; ?>" > </p> <p> <button type='submit'>提交</button> </p> </form> <?php include 'inc/footer.php'; ?>
loginout.php
<?php // 退出页 if(!isset($_COOKIE['user_id'])){ require './inc/function.php'; redirect_user(); } else { setcookie('user_id','',time()-3600); setcookie('email','',time()-3600); } $page_title = '退出'; include 'inc/header.php'; echo <<< HOME <h3>exit</h3> <hr> <p><a href='islogin.php'>登陆</a></p> HOME; include 'inc/footer.php';
Session
inc文件里的跟Cookie一样
demo2.php
<?php // 首页 // 启动会话 session_start(); $page_title = '首页'; // 头部 include 'inc/header.php'; if(isset($_SESSION['user_id']) && basename($_SERVER['PHP_SELF']) !='islogin.php'){ echo '<a href="loginout.php">退出</a><br>'; } else{ echo '<a href="islogin.php">登录</a><br>'; } // 底部 include 'inc/footer.php';
home.php
<?php // 登录成功 // 启动会话 session_start(); if(!isset($_SESSION['user_id'])){ require './inc/function.php'; redirect_user(); } $page_title = 'home'; include 'inc/header.php'; echo <<< HOME <h3>welcome</h3> <p>user_id:{$_SESSION['user_id']}</p> <p>email:{$_SESSION['email']}</p> <p><a href='loginout.php'>退出</a></p> HOME; include 'inc/footer.php';
isLogin.php
<?php // 登录页 // 启动会话 session_start(); // 请求判断 if($_SERVER['REQUEST_METHOD'] == 'POST'){ require './inc/db.php'; require './inc/function.php'; list($check,$data) = check_login($db,$_POST['name'],$_POST['password']); // check_login($db,'admin','123456'); if($check){ // 设置cookie // setcookie('user_id',$data['user_id']); // setcookie('email',$data['email']); $_SESSION['user_id'] = $data['user_id']; $_SESSION['email'] = $data['email']; // 跳转 redirect_user('home.php'); } else { $errors = $data; } // 关闭数据库 $db = null; } include 'login.php';
login.php
<?php // 登录模板 $page_title = 'login'; include 'inc/header.php'; // 错误信息 if(isset($errors) && !empty($errors)){ $error_msg = '<div>'; foreach($errors as $msg) { $error_msg .= $msg . '<br>'; } echo $error_msg."</div>"; } ?> <h4>用户登录</h4> <form action="islogin.php" method='post'> <p> <label for="name">姓名</label> <input type="text" name='name' id='name' value="<?php echo isset($_POST['name']) ? $_POST['name'] : ''; ?>" > </p> <p> <label for="password">手机号</label> <input type="password" name='password' id='password' value="<?php echo isset($_POST['password']) ? $_POST['password'] : ''; ?>" > </p> <p> <button type='submit'>提交</button> </p> </form> <?php include 'inc/footer.php'; ?>
loginout.php
<?php // 退出页 // 启动会话 session_start(); if(!isset($_SESSION['user_id'])){ require './inc/function.php'; redirect_user(); } else { // setcookie('user_id','',time()-3600); // setcookie('email','',time()-3600); session_destroy(); setcookie('PHPSESSID','',time()-36000); } $page_title = '退出'; include 'inc/header.php'; echo <<< HOME <h3>exit</h3> <hr> <p><a href='islogin.php'>登陆</a></p> HOME; include 'inc/footer.php';
总结
通过cookie与session会话机制的练习,体验一番client与server的简单交互过程,确实有趣的存储识别方式。