PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

博客列表 > PDO扩展连接mysql数据库,并实现用户登录时防sql注入的处理

PDO扩展连接mysql数据库,并实现用户登录时防sql注入的处理

赵大叔
赵大叔 原创
2022年01月23日 22:54:24 507浏览

用户登录

  • 使用预处理,防止sql注入攻击
  1. // 1.连接数据库
  2. $config = require_once __DIR__ .DIRECTORY_SEPARATOR .'connect.php';
  3. // var_dump($config);
  4. // pdo预处理接入
  5. // 准备一条预处理sql语句
  6. $sql = "SELECT * FROM `users` WHERE `username`= ? and `password` = ? ";
  7. // 准备要执行的语句,并返回语句对象
  8. $stmt = $pdo->prepare($sql);
  9. // 绑定参数到指定的变量名
  10. $para = [$name,$pwd];
  11. // 执行一条预处理语句
  12. $stmt->execute($para);
  13. $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
  14. // var_dump($res);
  15. if(empty($res)){
  16. echo (json_encode(array('code'=>1, 'msg'=>'用户名或密码不正确。')));
  17. };

用户登录

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  6. <style>
  7. *{margin:0;padding:0;border:0;font-size:100%;font:inherit;vertical-align:baseline;}
  8. article, aside, details, figcaption, figure,footer, header, hgroup, menu, nav, section {display: block;}
  9. ol,ul{list-style:none;margin:0px;padding:0px;}
  10. blockquote,q{quotes:none;}
  11. blockquote:before,blockquote:after,q:before,q:after{content:'';content:none;}
  12. table{border-collapse:collapse;border-spacing:0;}
  13. /*-- start editing from here --*/
  14. a{text-decoration:none;}
  15. .txt-rt{text-align:right;}/* text align right */
  16. .txt-lt{text-align:left;}/* text align left */
  17. .txt-center{text-align:center;}/* text align center */
  18. .float-rt{float:right;}/* float right */
  19. .float-lt{float:left;}/* float left */
  20. .clear{clear:both;}/* clear float */
  21. .pos-relative{position:relative;}/* Position Relative */
  22. .pos-absolute{position:absolute;}/* Position Absolute */
  23. .vertical-base{ vertical-align:baseline;}/* vertical align baseline */
  24. .vertical-top{ vertical-align:top;}/* vertical align top */
  25. nav.vertical ul li{ display:block;}/* vertical menu */
  26. nav.horizontal ul li{ display: inline-block;}/* horizontal menu */
  27. img{max-width:100%;}
  28. body {
  29. font-family: Verdana, serif;
  30. background: #fffef9;
  31. }
  32. .main-login {
  33. width: 35%;
  34. margin: 108px auto;
  35. background: #f2eada;
  36. background-size: cover;
  37. }
  38. .main-login > form {
  39. height: 368px;
  40. display: flex;
  41. flex-direction: column;
  42. justify-content: space-evenly;
  43. align-items: center;
  44. }
  45. .login-input{
  46. width: 94%;
  47. position: relative;
  48. }
  49. .login-input .iconfont{
  50. position: absolute;
  51. top: 8px;
  52. left: 26px;
  53. z-index: 2;
  54. font-size: 26px;
  55. color: #77787b;
  56. }
  57. input[type="text"],input[type="password"]{
  58. font-size: 1em;
  59. font-weight:100;
  60. height: 38px;
  61. width: 82%;
  62. margin-left: 18px;
  63. padding-left: 42px;
  64. position: absolute;
  65. }
  66. .login-text > ul {
  67. width: 100%;
  68. display: flex;
  69. justify-content: space-between;
  70. }
  71. .login-button{
  72. font-size: 1.2em;
  73. height: 38px;
  74. width: 48%;
  75. background-color: #1E9FFF;
  76. }
  77. .am-topbar-right{
  78. display: flex;
  79. flex-direction: column;
  80. width: 580px;
  81. height: 400px;
  82. margin: 200px auto;
  83. background-color: #fff;
  84. }
  85. </style>
  86. <title>LOGIN</title>
  87. </head>
  88. <body>
  89. <!-- main -->
  90. <div class="main-login">
  91. <form>
  92. <div class="login-input">
  93. <span class="iconfont icon-yonghuming"></span>
  94. <input class="text" type="text" name="username" placeholder="Username" required="">
  95. </div>
  96. <div class="login-input">
  97. <span class="iconfont icon-mima"></span>
  98. <input class="text" type="password" name="password" placeholder="Password" required="">
  99. </div>
  100. <div class="login-text" style="width: 86%">
  101. <ul>
  102. <li>
  103. <label class="anim">
  104. <input type="checkbox" class="checkbox" required="">
  105. <span> Remember me ?</span>
  106. </label>
  107. </li>
  108. <li><a href="/regist"> Regist account ?</a> </li>
  109. </ul>
  110. </div>
  111. <button type='button' name='btn' class="login-button">LOGIN</button>
  112. </form>
  113. </div>
  114. <!-- //main -->
  115. </body>
  116. </html>
  117. <script type="text/javascript" src="./JQuery3.5.1/jquery.3.5.1.js"></script>
  118. <script>
  119. $('button[name="btn"]').click(function() {
  120. var data = {};
  121. data.username = $.trim($('input[name="username"]').val());
  122. data.password = $.trim($('input[name="password"]').val());
  123. $.post('doLogin.php', data, function(res) {
  124. // console.log(res);
  125. alert(res.msg)
  126. }, 'json')
  127. })
  128. </script>

执行登录

  1. <?php
  2. // 后端可接受前端传过来的参数
  3. $name = isset($_POST['username']) ? $_POST['username'] : null;
  4. $pwd = isset($_POST['password']) ? $_POST['password'] : null;
  5. $pwd = password_hash('$pwd',PASSWORD_DEFAULT);
  6. // var_dump($name, $pwd);
  7. // die();
  8. // 1.连接数据库
  9. $config = require_once __DIR__ .DIRECTORY_SEPARATOR .'connect.php';
  10. // var_dump($config);
  11. // pdo预处理接入
  12. // 准备一条预处理sql语句
  13. $sql = "SELECT * FROM `users` WHERE `username`= ? and `password` = ? ";
  14. // 准备要执行的语句,并返回语句对象
  15. $stmt = $pdo->prepare($sql);
  16. // 绑定参数到指定的变量名
  17. $para = [$name,$pwd];
  18. // 执行一条预处理语句
  19. $stmt->execute($para);
  20. $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
  21. // var_dump($res);
  22. if(empty($res)){
  23. echo (json_encode(array('code'=>1, 'msg'=>'用户名或密码不正确。')));
  24. };

[https://help10086.cn/0121/login.php]

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议