Correction status:qualified
Teacher's comments:
SESSION
<?php //启动会话 session_start(); $page_title = '我是首页'; //导入页面的公共头部 include 'inc/header.php'; echo '<h2 style="color:red">我是首页</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 //启动会话 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 $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 (!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';
点击 "运行实例" 按钮查看在线实例
<?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 /** * 公共函数库 */ //用户登录成功之后的跳转 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 // 连接数据库 $dbc = mysqli_connect('127.0.0.1','root','root','php'); //判断连接是否成功 if (mysqli_connect_errno()) { die('连接失败'.mysqli_connect_error()); }
点击 "运行实例" 按钮查看在线实例
头部
<!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 $page_title = '我是首页'; //导入页面的公共头部 include 'inc/header.php'; echo '<h2 style="color:red">我是首页</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 $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; //导入页面的公共底部 include 'inc/footer.php';
点击 "运行实例" 按钮查看在线实例
退出
<?php //退出登录页面 //判断用户是否登录? 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'; //heredoc echo <<< "WELCOME" <h2 style="color:red">退出成功</h2> <p><a href="login.php">登录</a></p> WELCOME; //导入页面的公共底部 include 'inc/footer.php';
点击 "运行实例" 按钮查看在线实例