欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PHP实现简单请求分发器的注册与退出登录。

程序员文章站 2022-03-06 13:13:11
...

简单请求分发器的注册与退出登录

  • 1.建立数据库配置文件database.php
  1. <?php
  2. return [
  3. 'type' => $type ?? 'mysql',
  4. 'username' => $username ?? 'root',
  5. 'password' => $password ?? '',
  6. 'host' => $host ?? 'localhost',
  7. 'port' => $port ?? '3306',
  8. 'charset' => $charset ?? 'utf8',
  9. 'dbname' => 'user'
  10. ];
  • 2.建立connet.php连接数据库
  1. // 1. 把数据库连接配置文件引过来
  2. $config = require_once __DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'database.php';
  3. extract($config);
  4. // 2. 数据库的连接
  5. $dsn = sprintf('%s:host=%s;port=%s;charset=%s;dbname=%s', $type, $host, $port, $charset, $dbname);
  6. try {
  7. $pdo = new PDO($dsn, $username, $password);
  8. // var_dump($pdo);
  9. } catch (PDOException $e) {
  10. die('Connection error : ' . $e->getMessage());
  11. }
  • 3.建立,dosubmit.php对前端请求,进行分发处理。
  1. <?php
  2. // 控制器文件
  3. require 'common.php';
  4. // 后端可接受前端传过来的参数
  5. $name = isset($_POST['uname']) ? $_POST['uname'] : null;
  6. $pwd = isset($_POST['pwd']) ? $_POST['pwd'] : null;
  7. $type = strtolower($_GET['type']);
  8. var_dump($name, $pwd, $type);
  9. // 请求分发器 注册 登录 退出登录
  10. switch ($type) {
  11. case 'login':
  12. $res = checkName($name, $pwd);
  13. if ($res) {
  14. echo json_encode(['status' => 1, 'msg' => '登录成功'], 320);
  15. exit;
  16. }
  17. echo json_encode(['status' => 0, 'msg' => '用户名或密码错误'], 320);
  18. break;
  19. case 'logout':
  20. echo json_encode(['status' => 0, 'msg' => '退出登录'], 320);
  21. header("location:./2-login.php");
  22. session_destroy();
  23. break;
  24. case 'reg':
  25. $res = regusername($name, $pwd);
  26. if ($res) {
  27. echo json_encode(['status' => 1, 'msg' => '注册成功'], 320);
  28. exit;
  29. }
  30. echo json_encode(['status' => 0, 'msg' => '注册失败'], 320);
  31. break;
  32. // // 插入
  33. // case :'logout':
  34. }
  • 4.建立common.php文件,定义处理函数
  1. <?php
  2. session_start();
  3. // 模型文件 model
  4. //连接数据库
  5. require '1-connect.php';
  6. function checkName($name, $pwd)
  7. {
  8. $sql = "SELECT `username`,`password` FROM `user` WHERE `username` = ? AND `password` = ? ";
  9. // prepare准备阶段
  10. global $pdo;
  11. $stmt = $pdo->prepare($sql);
  12. $res = $stmt->execute([$name, $pwd]);
  13. if ($res) {
  14. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  15. // 验证通过
  16. if ($res) {
  17. $_SESSION['username'] = $res['username'];
  18. return true;
  19. } else {
  20. return false;
  21. }
  22. }
  23. }
  24. function regusername($name, $pwd)
  25. {
  26. $sql = "SELECT `username`,`password` FROM `user` WHERE `username` = ?";
  27. $sql_reg="INSERT INTO 'user'('username','password') VALUES (?,?)";
  28. global $pdo;
  29. $stmt = $pdo->prepare($sql);
  30. $res = $stmt->execute([$name]);
  31. if ($res) {
  32. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  33. if ($res) {
  34. //已存在用户 返回注册失败
  35. return false;
  36. } else {
  37. $stmt = $pdo->prepare($sql_reg);
  38. $stmt->debugDumpParams($sql_reg);
  39. $res = $stmt->execute([$name,$pwd]);
  40. if ($res) {
  41. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  42. if ($res) {
  43. return true;
  44. }
  45. else
  46. {
  47. var_dump("注册失败1");
  48. return false;
  49. }
  50. }
  51. var_dump("注册失败2");
  52. return false;
  53. }
  54. }
  55. }