Blogger Information
Blog 29
fans 0
comment 1
visits 19542
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
11月22日作业-SESSION
风清扬
Original
586 people have browsed it

11月22日作业:

  1. SESSION 登录实战
    部分代码

    登录

验证

首页文件:index.php

<?php
//开启会话
session_start();
// 为简化程序, 使用了一个中间层: 请求派发器,类似于框架的控制器, 对用户的请求进行集中处理

  1. // 1: 已登录: 显示出用户的登录信息, 显示退出按钮
  2. if (isset($_SESSION['name']) && $_SESSION['name'] === 'admin') {
  3. echo '用户: ' . $_SESSION['name'] . '已登录<br>';
  4. echo '<a href="dispatch.php?action=logout">退出</a>';
  5. } else {
  6. // 2. 未登录,就跳转到登录页面
  7. echo '<a href="dispatch.php?action=login">请登录</a>';
  8. }

派发器:dispatch.php

<?php
// 只需要在该脚本中打开会话即可, check.php/logout.php/login.php都是由它调用的, 不必重复开启
session_start();

// 连接数据库
require DIR . ‘/connect.php’;

// 获取请求参数
$action = isset($_GET[‘action’]) ? $_GET[‘action’] : ‘login’;
$action = htmlentities(strtolower(trim($action)));

// 请求分发
switch ($action) {
// 1. 登录页面
case ‘login’:
// 加载登录表单
include DIR . ‘/login.php’;
break;

  1. // 2. 验证登录
  2. case 'check':
  3. include __DIR__ . '/check.php';
  4. break;
  5. // 3. 退出登录
  6. case 'logout':
  7. include __DIR__ . '/logout.php';
  8. break;
  9. // 默认操作
  10. default:
  11. header('Location: index.php');
  12. echo '<script>location.assign("index.php");</script>';

}
登陆页面:login.php

<?php
// 防止用户重复登录
if (isset($_SESSION[‘name’])) {
echo ‘<script>alert(“不要重复登录”);location.assign(“index.php”);</script>‘;
}
?>

<!doctype html>

<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<body>
<h3>用户登录</h3>
<form action="dispatch.php?action=check" method="post" onsubmit="return isEmpty();">
<p>
<label for="phone">手机:</label>
<input type="phone" name="phone" id="phone">
</p>

  1. <p>
  2. <label for="password">密码:</label>
  3. <input type="password" name="password" id="password">
  4. </p>
  5. <p>
  6. <button>提交</button>
  7. </p>
  8. </form>
  9. <script>
  10. function isEmpty() {
  11. var phone = document.getElementById('phone').value;
  12. var password = document.getElementById('password').value;
  13. if (phone.length=== 0 || password.length===0) {
  14. alert('手机和密码不能为空');
  15. return false;
  16. }
  17. }
  18. </script>
  19. </body>

</html>
验证登陆:check.php

<?php
// 1.判断用户的请求类型是否正确?
if ($_SERVER[‘REQUEST_METHOD’] === ‘POST’) {
// 2.获取表单数据
$phone = $_POST[‘phone’];
$password = sha1($_POST[‘password’]);

  1. // 3. 用用户表user.dbf进行验证
  2. $sql = 'SELECT * FROM `user` WHERE `phone` = :phone AND `password` = :password LIMIT 1';
  3. $stmt = $pdo->prepare($sql);
  4. $stmt->execute(['phone'=>$phone, 'password'=>$password]);
  5. $user = $stmt->fetch(PDO::FETCH_ASSOC);
  6. // 4. 判断验证的结果
  7. if (false === $user) {
  8. // 验证失败,返回上一下页面
  9. echo '<script>alert("验证失败");history.back();</script>';
  10. die;
  11. }
  12. // 验证成功,将用户的信息写到session
  13. $_SESSION['name'] = $user['name'];
  14. echo '<script>alert("登录成功");location.assign("index.php");</script>';
  15. exit;

} else {
die(‘请求类型错误’);
}
退出登陆:logout.php

<?php
// 必须在用户已经登录的情况下再退出
if (isset($_SESSION[‘name’])) {
session_destroy();
echo ‘<script>alert(“退出成功”);location.assign(“index.php”);</script>‘;
} else {
// 要求用户先登录
echo ‘<script>alert(“请先登录”);location.assign(“login.php”);</script>‘;
}

  1. 练熟pdo操作,增删查改(手写)
Correcting teacher:查无此人查无此人

Correction status:qualified

Teacher's comments:完成的不错。pdo操作(手写),应该多写点pdo的方法。继续努力
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post