Correction status:Uncorrected
Teacher's comments:
1 , 编程: cookie实现用户登录与验证
<?php $page_title = '我是首页'; include 'mom/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 'mom/footer.php';
点击 "运行实例" 按钮查看在线实例
<?php $page_title = '用户登录'; include 'mom/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> <lable for="email">邮箱</lable> <input type="email" name="email" id="email" value="<?php echo isset($_POST['email']) ? $_POST['email'] :'' ?>"> </p> <p> <lable for="password">密码</lable> <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 'mom/footer.php'?>
<?php if ($_SERVER['REQUEST_METHOD'] == 'POST'){ require 'mom/function.php'; require 'mom/mysql.php'; list($check,$data) = check_login($dbs,$_POST['email'],$_POST['password']); if ($check){ // 开始设置cookie setcookie('id',$data['id']); setcookie('name',$data['name']); // 设置跳转 redirect_user('loggedin.php'); }else{ $errors = $data; }mysqli_close($dbs); } include 'login_page.php';
<?php if (!isset($_COOKIE['id'])){ require 'mom/function.php'; redirect_user(); } $page_title = '登录成功'; include 'mom/header.php'; echo <<< "WELCOME" <h2 style="color: red;">登录成功</h2> <p>欢迎您:亲爱的{$_COOKIE['name']}</p> <p><a href="logout.php">退出</a></p> WELCOME; include 'mom/footer.php';
<?php if (!isset($_COOKIE['id'])){ require 'mom/function.php'; redirect_user(); }else{ setcookie('id','',time()-3600); setcookie('name','',time()-3600); } $page_title = '退出成功'; include 'mom/header.php'; echo <<< "WELCOME" <h2 style="color: red;">退出成功</h2> <p><a href="login.php">登录</a></p> WELCOME; include 'mom/footer.php';
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> <?php echo isset($page_title) ? $page_title : '默认标题'; ?> </title> </head> <body> <h1>我是你们的头部</h1>
<h1>我是你们的底部</h1> </body> </html>
<?php $dbs = mysqli_connect('127.0.0.1','root','root','php'); if (mysqli_connect_errno()){ die('连接失败'.mysqli_connect_error()); }
<?php function check_login($dbs,$email='',$password){ $errors = []; if (empty($email)){ $errors[] = '邮箱不能为空'; }else{ $e = mysqli_escape_string($dbs,trim($email)); } if (empty($password)){ $errors[] = '密码不能为空'; }else{ $p = mysqli_escape_string($dbs,trim($password)); } if (empty($errors)){ $sql = "SELECT `id`,`name` FROM `user` WHERE `email`= '$e 'AND `password`= sha1('$p')"; // die($sql); $res = mysqli_query($dbs,$sql); // var_dump($res);die; // echo mysqli_num_rows($res);die; if (mysqli_num_rows($res) == 1){ $row = mysqli_fetch_array($res,MYSQLI_ASSOC); // print_r($row);die; return [true,$row]; }else{ $errors[] = '邮箱或密码不正确,请重新输入'; }return [false,$errors]; } } function redirect_user($page = 'index.php'){ $url = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']); $url = rtrim($url,'/\\'); $url.= '/'.$page; header('location:'.$url); exit(); }
2 , 编程: session实现用户登录与验证
以下添加需要更改为session回话的页面 :
<?php session_start(); $page_title = '我是首页'; include 'mom/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 'mom/footer.php';
<?php session_start(); if ($_SERVER['REQUEST_METHOD'] == 'POST'){ require 'mom/function.php'; require 'mom/mysql.php'; list($check,$data) = check_login($dbs,$_POST['email'],$_POST['password']); if ($check){ // 开始设置cookie // setcookie('id',$data['id']); // setcookie('name',$data['name']); // 设置session $_SESSION['id'] = $data['id']; $_SESSION['name'] = $data['name']; // 设置跳转 redirect_user('loggedin.php'); }else{ $errors = $data; }mysqli_close($dbs); } include 'login_page.php';
<?php session_start(); if (!isset($_SESSION['id'])){ require 'mom/function.php'; redirect_user(); } $page_title = '登录成功'; include 'mom/header.php'; echo <<< "WELCOME" <h2 style="color: red;">登录成功</h2> <p>欢迎您:亲爱的{$_SESSION['name']}</p> <p><a href="logout.php">退出</a></p> WELCOME; include 'mom/footer.php';
<?php session_start(); if (!isset($_SESSION['id'])){ require 'mom/function.php'; redirect_user(); }else{ // setcookie('id','',time()-3600); // setcookie('name','',time()-3600); session_destroy(); setcookie('PHPSESSION','',time()-3600); } $page_title = '退出成功'; include 'mom/header.php'; echo <<< "WELCOME" <h2 style="color: red;">退出成功</h2> <p><a href="login.php">登录</a></p> WELCOME; include 'mom/footer.php';
3 , 手写: cookie与session会话机制的优缺点分析
回答 : session会话机制相比较cookie会话机制而言更加安全 , 因为session是储存在服务器中 ;
所以除却安全性之外 , 还更加的易于操作和管理 !