Blogger Information
Blog 17
fans 1
comment 0
visits 20046
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP网页的登录与注册示例
大A
Original
2242 people have browsed it

示例介绍

一个简单的注册登录验证页面
用户名,邮箱,电话是不可重复注册的

源码

数据库操作与验证部分

db.php

  1. <?php
  2. /*
  3. * 创建sql表
  4. * CREATE TABLE `usertab` (
  5. `id` int(3) NOT NULL AUTO_INCREMENT COMMENT '主键',
  6. `name` varchar(30) CHARACTER SET utf8 NOT NULL COMMENT '用户名',
  7. `password` varchar(40) CHARACTER SET utf8 NOT NULL COMMENT '密码',
  8. `email` varchar(30) CHARACTER SET utf8 NOT NULL COMMENT '邮箱',
  9. `tel` varchar(30) CHARACTER SET utf8 NOT NULL COMMENT '电话',
  10. `time` int(10) unsigned NOT NULL COMMENT '注册时间',
  11. UNIQUE KEY (`name`),
  12. UNIQUE KEY (`email`),
  13. UNIQUE KEY (`tel`),
  14. PRIMARY KEY (`id`))
  15. */
  16. /*
  17. * @Author: 数据库类
  18. * @Date: 2020-05-08 14:04:42
  19. * @LastEditTime: 2020-05-09 18:04:54
  20. * @LastEditors: Please set LastEditors
  21. * @Description: In User Settings Edit
  22. * @FilePath: \test\db.php
  23. */
  24. class Darabase
  25. {
  26. private $config =
  27. [
  28. 'host' => 'mysql:host=localhost;dbname=phpedu;',
  29. 'username' => 'root',
  30. 'password' => 'root'
  31. ];
  32. private $pdo_obj;
  33. public function __construct()
  34. {
  35. $this->pdo_obj = new PDO(
  36. $this->config['host'],
  37. $this->config['username'],
  38. $this->config['password'],
  39. );
  40. }
  41. /**
  42. * 注册新用户,成功返回true,失败返回false
  43. *
  44. * @param [type] $name 用户名
  45. * @param [type] $pass 密码
  46. * @param [type] $email 邮箱
  47. * @param [type] $tel 电话
  48. * @param [type] $time 注册时间
  49. * @return bool
  50. *
  51. */
  52. public function reguser($name, $pass, $email, $tel, $time)
  53. {
  54. $pass = sha1($pass);
  55. $sql = "INSERT `usertab` SET
  56. `name`='$name',
  57. `password`='$pass',
  58. email='$email',
  59. tel='$tel',
  60. `time`=$time";
  61. $obj = $this->pdo_obj->prepare($sql);
  62. $obj->execute();
  63. if ($obj->rowCount() > 0) :
  64. return true;
  65. else :
  66. return false;
  67. endif;
  68. }
  69. /**
  70. * 数据库查询,返回查询的结果或false
  71. *
  72. * @param [string] $result 要求返回的查询结果
  73. * @param [string] $field 要查询的字段
  74. * @param [string] $value 查询的字段的值
  75. * @return mixed
  76. */
  77. public function search($result, $field, $value)
  78. {
  79. $sql = "SELECT `$result` FROM `usertab` WHERE `$field`='$value'";
  80. $obj = $this->pdo_obj->prepare($sql);
  81. $obj->execute();
  82. $result = $obj->fetch()[0];
  83. return $result ? $result : false;
  84. }
  85. /**
  86. * 查询用户是否存在,存在返回true,不存在返回false
  87. *
  88. * @param [string] $name 用户名
  89. * @param [string] $pass 用于接收密码的变量
  90. * @return bool
  91. */
  92. public function searchName($name, &$pass = null)
  93. {
  94. $pass = $this->search('password', 'name', $name);
  95. var_dump($pass);
  96. return $pass ? true : false;
  97. }
  98. /**
  99. * 登录验证,成功返回true,失败返回false
  100. *
  101. * @param [string] $name 用户名
  102. * @param [string] $pass 密码
  103. * @return bool
  104. */
  105. public function login($name, $pass)
  106. {
  107. $pass = sha1($pass);
  108. return $this->searchName($name, $rpass) ?
  109. $pass === $rpass ? true : false
  110. :
  111. false;
  112. }
  113. }

handle.php

  1. <?php
  2. require('db.php');
  3. $action = filter_input(INPUT_GET, 'action');
  4. if ($action === null) die('参数非法');
  5. //var_dump($_POST, $action);
  6. switch ($action) {
  7. case 'reg':
  8. if (!$_POST) die('参数非法');
  9. $data = new Darabase();
  10. if ($data->search('name', 'name', $_POST['name'])) header('Location:reg.php?message=用户名已存在');
  11. if ($data->search('email', 'email', $_POST['email'])) header('Location:reg.php?message=邮箱已注册');
  12. if ($data->search('tel', 'tel', $_POST['tel'])) header('Location:reg.php?message=手机号已注册');
  13. if ($data->reguser(
  14. $_POST['name'],
  15. $_POST['password'],
  16. $_POST['email'],
  17. $_POST['tel'],
  18. (string) time()
  19. )) :
  20. echo '注册成功!<br><a href="login.php">>>>登录</a>';
  21. else :
  22. echo '注册失败';
  23. endif;
  24. break;
  25. case 'login':
  26. if (!$_POST) die('参数非法');
  27. $data = new Darabase();
  28. if ($data->login($_POST['name'], $_POST['password'])) :
  29. session_start();
  30. $pass = sha1($_POST['password']);
  31. $_SESSION['name'] = $_POST['name'];
  32. $_SESSION['password'] = $pass;
  33. setcookie('name', $_POST['name'], (time() + 360 * 24));
  34. setcookie('pass', $pass, (time() + 360 * 24));
  35. header('location:index.php');
  36. else :
  37. header('Location:login.php?message=账号或密码错误');
  38. endif;
  39. break;
  40. case 'out':
  41. session_start();
  42. session_destroy();
  43. setcookie('name', null, 0);
  44. header('location:index.php');
  45. break;
  46. default:
  47. echo '参数非法';
  48. break;
  49. }

HTML部分

index.php

  1. <?php
  2. if ($_COOKIE['name'])
  3. session_start();
  4. ?>
  5. <!DOCTYPE html>
  6. <html lang="en">
  7. <head>
  8. <meta charset="UTF-8">
  9. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  10. <link rel="stylesheet" href="index.css">
  11. <title>首页</title>
  12. </head>
  13. <body>
  14. <section>
  15. <ul class="nav black">
  16. <li><a href="">主页</a></li>
  17. <li><a href=""></a></li>
  18. <li><a href=""></a></li>
  19. <li><a href=""></a></li>
  20. <li><a href=""></a></li>
  21. <?php if (
  22. $_COOKIE['name'] . $_COOKIE['pass'] === $_SESSION['name'] . $_SESSION['password']
  23. && $_COOKIE['name']
  24. ) : ?>
  25. <li><a href="handle.php?action=out">退出</a></li>
  26. <? else : ?>
  27. <li><a href="login.php">登录</a></li>
  28. <? endif; ?>
  29. </ul>
  30. </section>
  31. </body>
  32. </html>

login.php

  1. <?php $mess = filter_input(INPUT_GET, 'message'); ?>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <link rel="stylesheet" href="style.css">
  8. <title>首页</title>
  9. </head>
  10. <body>
  11. <div class='login'>
  12. <h2 style='text-align: center;padding-top:20px'>用户登录</h3>
  13. <form action="handle.php?action=login" method="post">
  14. <div>
  15. <label for="input_name" class=lab>用户名:</label>
  16. <input type="text" name="name" id="input_name">
  17. </div>
  18. <div>
  19. <label for="input_pass" class=lab>密码:</label>
  20. <input type="password" name="password" id="input_pass">
  21. </div>
  22. <div>
  23. <input type="submit" value="登录" id=sub>
  24. </div>
  25. <div>
  26. <?php echo "<font style='color:#FF0000;'>" . $mess . '</font>'; ?> <a style='padding-left: 30%;' href="reg.php">注册>>></a>
  27. </div>
  28. </form>
  29. </div>
  30. </body>
  31. </html>

reg.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="stylesheet" href="style.css">
  7. <title>首页</title>
  8. </head>
  9. <body>
  10. <?php $mess = filter_input(INPUT_GET, 'message'); ?>
  11. <div class='login' style='height:460px;'>
  12. <h2 style='text-align: center;padding-top:20px'>用户注册</h2>
  13. <form action="handle.php?action=reg" method="post">
  14. <div>
  15. <label for="input_name" class=lab>用户名:</label>
  16. <input type="text" name="name" id="input_name">
  17. </div>
  18. <div>
  19. <label for="input_pass" class=lab>密码:</label>
  20. <input type="password" name="password" id="input_pass">
  21. </div>
  22. <div>
  23. <label for="input_pass1" class=lab>重复密码:</label>
  24. <input type="password" name="" id="input_pass1">
  25. </div>
  26. <div>
  27. <label for="input_email" class=lab>邮箱:</label>
  28. <input type="email" name="email" id="input_email">
  29. </div>
  30. <div>
  31. <label for="input_tel" class=lab>手机号:</label>
  32. <input type="text" name="tel" id="input_tel">
  33. </div>
  34. <div>
  35. <input type="submit" value="注册" id=sub>
  36. </div>
  37. </form>
  38. <?php echo "<h4 style='text-align:center;color:#FF0000;'>" . $mess . '</h4>'; ?>
  39. </div>
  40. </body>
  41. </html>

css样式部分

index.css

  1. * {
  2. margin : 0;
  3. padding : 0;
  4. list-style-type: none;
  5. }
  6. .nav a {
  7. display : inline-block;
  8. -webkit-transition: all 0.2s ease-in;
  9. -moz-transition : all 0.2s ease-in;
  10. -o-transition : all 0.2s ease-in;
  11. -ms-transition : all 0.2s ease-in;
  12. transition : all 0.2s ease-in;
  13. }
  14. .black {
  15. background: #2c2c2c;
  16. box-shadow: 0 7px 0 #0b0b0b;
  17. }
  18. .black li::before {
  19. left : 0;
  20. background: -moz-linear-gradient(top, #2c2c2c, #000 50%, #2c2c2c);
  21. background: -webkit-linear-gradient(top, #2c2c2c, #000 50%, #2c2c2c);
  22. background: -o-linear-gradient(top, #2c2c2c, #000 50%, #2c2c2c);
  23. background: -ms-linear-gradient(top, #2c2c2c, #000 50%, #2c2c2c);
  24. background: linear-gradient(top, #2c2c2c, #000 50%, #2c2c2c);
  25. }
  26. body {
  27. background: #ebebeb;
  28. }
  29. .nav {
  30. width : 100%;
  31. height : 50px;
  32. font : bold 0/50px Arial;
  33. text-align : center;
  34. margin : 40px auto 0;
  35. border-radius: 8px;
  36. }
  37. .nav a:hover {
  38. -webkit-transform: rotate(10deg);
  39. -moz-transform : rotate(10deg);
  40. -o-transform : rotate(10deg);
  41. -ms-transform : rotate(10deg);
  42. transform : rotate(10deg);
  43. }
  44. .nav li {
  45. position : relative;
  46. display : inline-block;
  47. padding : 0 16px;
  48. font-size : 13px;
  49. text-shadow: 1px 2px 4px rgba(0, 0, 0, .5);
  50. list-style : none outside none;
  51. }
  52. .nav a,
  53. .nav a:hover {
  54. color : #fff;
  55. text-decoration: none;
  56. }

style.css

  1. div.login {
  2. margin : auto;
  3. background-color: cornflowerblue;
  4. height : 310px;
  5. width : 300px;
  6. line-height : 50px;
  7. position : relative;
  8. }
  9. label.lab {
  10. padding-inline-start: 25px;
  11. display : block;
  12. width : 70px;
  13. display : inline-block;
  14. }
  15. #sub {
  16. background-color: honeydew;
  17. height : 30px;
  18. width : 250px;
  19. margin : auto;
  20. display : block;
  21. margin-top : 30px;
  22. }

源码下载

https://www.90pan.com/b1872334

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:规范的做法, 应该是将数据库连接参数通过构造方法传入, 试试看
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!