Correction status:Uncorrected
Teacher's comments:
cookie实现用户登录与验证代码实例如下:
<?php // cookie //1.设置cookie //setcookie('username','admin',time()+3600); //2.查看cookie: 超全局系统变量[数组]: $_COOKIE //echo '<br>',$_COOKIE['username']; //3.删除cookie setcookie('username','admin',time()-3600); echo '<br>',$_COOKIE['username'];
点击 "运行实例" 按钮查看在线实例
<?php // 连接数据库 $dbc = mysqli_connect('127.0.0.1','root','root','php'); //判断连接是否成功 if (mysqli_connect_errno()) { die('连接失败'.mysqli_connect_error()); }
点击 "运行实例" 按钮查看在线实例
<?php /** * 公共函数库 */ //用户登录成功之后的跳转 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 `password`=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]; } }
点击 "运行实例" 按钮查看在线实例
<?php $page_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 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('id',$data['id']); setcookie('name',$data['name']); //跳转 redirect_user('loggedin.php'); } else { $errors = $data; } //关闭 mysqli_close($dbc); } include 'login_page.php';
点击 "运行实例" 按钮查看在线实例
?php //登录成功页面 //判断用户是否登录? 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;
点击 "运行实例" 按钮查看在线实例
session实现用户登录与验证代码实例如下:
<?php // session // 开启会话,在浏览上创建一个基于cookie的session_id session_start(); //创建sessoin $_SESSION['user_name'] = 'admin'; $_SESSION['user_id'] = 120; //更新 $_SESSION['user_name'] = 'peter zhu'; // 查看 echo $_SESSION['user_name']; //删除 session_destroy(); //服务器上的sessoin删除了 setcookie('PHPSESSID','',time()-3600); //浏览器上的session_id删除
点击 "运行实例" 按钮查看在线实例
<?php // 连接数据库 $dbc = mysqli_connect('127.0.0.1','root','root','php'); //判断连接是否成功 if (mysqli_connect_errno()) { die('连接失败'.mysqli_connect_error()); }
点击 "运行实例" 按钮查看在线实例
<?php /** * 公共函数库 */ //用户登录成功之后的跳转 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 `password`=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]; } }
点击 "运行实例" 按钮查看在线实例
<?php $page_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 //启动会话 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) { //设置session $_SESSION['id'] = $data['id']; $_SESSION['name'] = $data['name']; //跳转 redirect_user('loggedin.php'); } else { $errors = $data; } //关闭 mysqli_close($dbc); } include 'login_page.php';
点击 "运行实例" 按钮查看在线实例
<?php //启动会话 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 //启动会话 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('PHPSESSID','',time()-3600); } $page_title = '退出登录'; //导入页面的公共头部 include 'inc/header.php'; //heredoc echo <<< "WELCOME" <h2 style="color:red">退出成功</h2> <p><a href="login.php">登录</a></p> WELCOME; //导入页面的公共底部 include 'inc/footer.php';
点击 "运行实例" 按钮查看在线实例
手抄问答及课堂总结照片如下: