Correction status:qualified
Teacher's comments:
inc文件内容:
<?php /** * Created by PhpStorm. * User: 714326675 * Date: 2018/9/2 * Time: 21:41 */ //连接数据库 $dbc=mysqli_connect('127.0.0.1','root','','test'); //判断连接是否成功 if(mysqli_connect_errno()){ die('连接失败'.mysqli_connect_error()); }
点击 "运行实例" 按钮查看在线实例
<?php /** * Created by PhpStorm. * User: 714326675 * Date: 2018/9/2 * Time: 21:47 */ //用户登录成功之后的跳转 function redirect_user($page='index.php'){ //默认的url $url='http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']); //去掉url右边的斜线 $url=rtrim($url,'/\\'); //添加上当前的脚本名称 $url.='/'.$page; //挑转到指定地址 header('Location:'.$url); exit(); } //验证用户登录 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)){ //根据邮箱和密码进行验证,并返回id和name $sql = "SELECT `id`,`name` FROM `user` WHERE `email`='$e' AND `passwd`=sha1('$p')"; //执行查询 $res=mysqli_query($dbc,$sql); if(mysqli_num_rows($res) == 1){ //返回查询结果 $row=mysqli_fetch_array($res,MYSQLI_ASSOC); return [true,$row]; }else{ $errors[]='邮箱或密码不正确,请重新输入'; } return [false,$errors]; } }
点击 "运行实例" 按钮查看在线实例
<!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> <h1>我是头部</h1>
点击 "运行实例" 按钮查看在线实例
cookie:
首页:
<?php /** * Created by PhpStorm. * User: 714326675 * Date: 2018/9/2 * Time: 21:26 */ //导入页面头部 $page_title='首页'; include 'inc/header.php'; echo '<h2>我是首页</h2>'; //判断用户是否登录 if(isset($_COOKIE['id']) && basename($_SERVER['PHP_SELF']) !='logout.php'){ echo '<a href="logout.php">退出</a>'; }else{ echo '<a href="login.php">登录</a>'; } //导入页面尾部 include 'inc/footer.php'; ?>
点击 "运行实例" 按钮查看在线实例
登录页:
<?php /** * Created by PhpStorm. * User: 714326675 * Date: 2018/9/2 * Time: 22:15 */ 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){ //$data['id']和$data['name']为 $row=mysqli_fetch_array($res,MYSQLI_ASSOC);的结果集 //设置cookie setcookie('id',$data['id']); setcookie('name',$data['name']); //挑转 redirect_user('loggedin.php'); }else{ $errors=$data; } //关闭 mysqli_close($dbc); } include 'login_page.php';
点击 "运行实例" 按钮查看在线实例
<?php /** * Created by PhpStorm. * User: 714326675 * Date: 2018/9/3 * Time: 11:12 */ $paga_title='用户登录'; include 'inc/header.php'; //错误信息显示 if(isset($errors) && !empty($errors)){ $errors_msg='<p style="color:red;">'; foreach ($errors as $msg){ $errors_msg.=$msg.'<br>'; } echo $errors_msg.'</p>'; } ?> <h2 style="color:red;">用户登录</h2> <form action="login.php" method="POST"> <p><label for="email">邮箱:</label> <input type="email" name="email" id="email" value="<?php echo isset($_POST['email'])? $_POST['email']:'' ?>"> </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" name="submit" id="submit">登录</button> </p> </form> <?php include 'inc/footer.php'; ?>
点击 "运行实例" 按钮查看在线实例
登录成功内容:
<?php /** * Created by PhpStorm. * User: 714326675 * Date: 2018/9/3 * Time: 11:26 */ //判断用户是否登录 if(!isset($_COOKIE['id'])){ require 'inc/function.php'; redirect_user(); } $page_title='登录成功'; //导入公共头部 include 'inc/header.php'; //heredoc echo <<<WELCOME <h2 style="color:red;">登录成功</h2> <p>欢迎您:亲爱的{$_COOKIE['name']}</p> <p><a href="logout.php">退出</a></p> WELCOME; //导入公共底部 include 'inc/footer.php';
点击 "运行实例" 按钮查看在线实例
退出登录页:
<?php /** * Created by PhpStorm. * User: 714326675 * Date: 2018/9/3 * Time: 11:31 */ //退出登录页面 //判断是否登录 if(!isset($_COOKIE['id'])){ require 'inc/function.php'; redirect_user(); }else{ //退出登录,删除cookie setcookie('id','',time()-3600); setcookie('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部分:
首页:
<?php /** * Created by PhpStorm. * User: 714326675 * Date: 2018/9/2 * Time: 21:26 */ session_start(); //导入页面头部 $page_title='首页'; include 'inc/header.php'; echo '<h2>我是首页</h2>'; //判断用户是否登录 if(isset($_SESSION['id']) && basename($_SERVER['PHP_SELF']) !='logout.php'){ echo '<a href="logout.php">退出</a>'; }else{ echo '<a href="login.php">登录</a>'; } //导入页面尾部 include 'inc/footer.php'; ?>
点击 "运行实例" 按钮查看在线实例
登录页:
<?php /** * Created by PhpStorm. * User: 714326675 * Date: 2018/9/2 * Time: 22:15 */ 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){ //$data['id']和$data['name']为 $row=mysqli_fetch_array($res,MYSQLI_ASSOC);的结果集 //设置SESSION $_SESSION['id']=$data['id']; $_SESSION['name']=$data['name']; //挑转 redirect_user('loggedin.php'); }else{ $errors=$data; } //关闭 mysqli_close($dbc); } include 'login_page.php';
点击 "运行实例" 按钮查看在线实例
<?php /** * Created by PhpStorm. * User: 714326675 * Date: 2018/9/3 * Time: 11:12 */ $paga_title='用户登录'; include 'inc/header.php'; //错误信息显示 if(isset($errors) && !empty($errors)){ $errors_msg='<p style="color:red;">'; foreach ($errors as $msg){ $errors_msg.=$msg.'<br>'; } echo $errors_msg.'</p>'; } ?> <h2 style="color:red;">用户登录</h2> <form action="login.php" method="POST"> <p><label for="email">邮箱:</label> <input type="email" name="email" id="email" value="<?php echo isset($_POST['email'])? $_POST['email']:'' ?>"> </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" name="submit" id="submit">登录</button> </p> </form> <?php include 'inc/footer.php'; ?>
点击 "运行实例" 按钮查看在线实例
登录成功页:
<?php /** * Created by PhpStorm. * User: 714326675 * Date: 2018/9/3 * Time: 11:26 */ session_start(); //判断用户是否登录 if(!isset($_SESSION['id'])){ require 'inc/function.php'; redirect_user(); } $page_title='登录成功'; //导入公共头部 include 'inc/header.php'; //heredoc echo <<<WELCOME <h2 style="color:red;">登录成功</h2> <p>欢迎您:亲爱的{$_SESSION['name']}</p> <p><a href="logout.php">退出</a></p> WELCOME; //导入公共底部 include 'inc/footer.php';
点击 "运行实例" 按钮查看在线实例
退出登录页:
<?php /** * Created by PhpStorm. * User: 714326675 * Date: 2018/9/3 * Time: 11:31 */ //退出登录页面 //启动会话 session_start(); //判断是否登录 if(!isset($_SESSION['id'])){ require 'inc/function.php'; redirect_user(); }else{ //退出登录,删除SESSION // setcookie('id','',time()-3600); //setcookie('name','',time()-3600);] session_destroy();//服务器上的session信息消除 setcookie('PHPSESSION','',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('名字','值');销毁cookie:setcookie('名字','值',time()-3600); -3600就是过期了一小时 +3600的话就是一小时后过期
session:开启session:session_start(); 销毁session:session_destroy();再把客-户端的cookie销毁:settcookie('名字','值',time()-3600);