小言_互联网的博客

PHP案例:连接数据库实现登录功能

749人阅读  评论(0)

目录

一、创建数据库与表

1、创建数据库 - simonshop

2、创建用户表 - t_user

二、创建PHP项目login

1、用户实体类 - User.php

2、数据源类 - DataSource.php

3、用户数据访问类 - UserDao.php

4、登录页面 - login.php

5、登录处理页面 - doLogin.php

6、登录成功页面 - succes.php

7、登录失败页面 - failure.php

8、运行程序,访问http://localhost/login/login.php

9、显示单个用户页面  - showOneUser.php

10、运行程序,访问http://localhost/login/showOneUser.php

11、显示全部用户页面 - showAllUsers.php

11、运行程序,访问http://localhost/login/showAllUsers.php​


一、创建数据库与表

1、创建数据库 - simonshop

 

2、创建用户表 - t_user

二、创建PHP项目login

1、用户实体类 - User.php


  
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: howard
  5. * Date: 2017/4/12
  6. * Time: 13:54
  7. */
  8. namespace net\ hw\ bean;
  9. class User
  10. {
  11. private $id;
  12. private $username;
  13. private $password;
  14. private $telephone;
  15. private $registerTime;
  16. private $popedom;
  17. /**
  18. * @return mixed
  19. */
  20. public function getId()
  21. {
  22. return $this->id;
  23. }
  24. /**
  25. * @param mixed $id
  26. */
  27. public function setId($id)
  28. {
  29. $this->id = $id;
  30. }
  31. /**
  32. * @return mixed
  33. */
  34. public function getUsername()
  35. {
  36. return $this->username;
  37. }
  38. /**
  39. * @param mixed $username
  40. */
  41. public function setUsername($username)
  42. {
  43. $this->username = $username;
  44. }
  45. /**
  46. * @return mixed
  47. */
  48. public function getPassword()
  49. {
  50. return $this->password;
  51. }
  52. /**
  53. * @param mixed $password
  54. */
  55. public function setPassword($password)
  56. {
  57. $this->password = $password;
  58. }
  59. /**
  60. * @return mixed
  61. */
  62. public function getTelephone()
  63. {
  64. return $this->telephone;
  65. }
  66. /**
  67. * @param mixed $telephone
  68. */
  69. public function setTelephone($telephone)
  70. {
  71. $this->telephone = $telephone;
  72. }
  73. /**
  74. * @return mixed
  75. */
  76. public function getRegisterTime()
  77. {
  78. return $this->registerTime;
  79. }
  80. /**
  81. * @param mixed $registerTime
  82. */
  83. public function setRegisterTime($registerTime)
  84. {
  85. $this->registerTime = $registerTime;
  86. }
  87. /**
  88. * @return mixed
  89. */
  90. public function getPopedom()
  91. {
  92. return $this->popedom;
  93. }
  94. /**
  95. * @param mixed $popedom
  96. */
  97. public function setPopedom($popedom)
  98. {
  99. $this->popedom = $popedom;
  100. }
  101. function __toString()
  102. {
  103. return "User{id=$this->id, username='$this->username',
  104. password='$this->password', telephone='$this->telephone',
  105. registerTime='$this->registerTime', popedom=$this->popedom}";
  106. }
  107. }
  108. ?>

2、数据源类 - DataSource.php


  
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: howard
  5. * Date: 2017/4/12
  6. * Time: 13:56
  7. */
  8. namespace net\ hw\ util;
  9. class DataSource
  10. {
  11. public function getConn()
  12. {
  13. $host = "localhost";
  14. $username = "root";
  15. $password = "root";
  16. $dbname = "simonshop";
  17. $conn = mysqli_connect($host, $username, $password, $dbname);
  18. if ($conn->connect_errno)
  19. {
  20. die( "连接MySQL数据库失败!" . $conn->connect_error);
  21. }
  22. return $conn;
  23. }
  24. }

3、用户数据访问类 - UserDao.php


  
  1. <?php
  2. namespace net\ hw\ dao;
  3. require_once( "/net/hw/bean/User.php");
  4. require_once( "/net/hw/util/DataSource.php");
  5. use \ net\ hw\ bean\ User;
  6. use \ net\ hw\ util\ DataSource;
  7. class UserDao
  8. {
  9. public function findUserById($id)
  10. {
  11. $dataSource = new DataSource();
  12. $conn = $dataSource->getConn();
  13. $sql = "select * from t_user where id = $id";
  14. $result = $conn->query($sql);
  15. $row = $result->fetch_array(MYSQLI_ASSOC);
  16. $user = new User();
  17. $user->setId($row[ 'id']);
  18. $user->setUsername($row[ 'username']);
  19. $user->setPassword($row[ 'password']);
  20. $user->setTelephone($row[ 'telephone']);
  21. $user->setRegisterTime($row[ 'register_time']);
  22. $user->setPopedom($row[ 'popedom']);
  23. return $user;
  24. }
  25. public function findAllUsers()
  26. {
  27. $dataSource = new DataSource();
  28. $conn = $dataSource->getConn();
  29. $sql = "select * from t_user";
  30. $result = $conn->query($sql);
  31. $i = 0;
  32. $users = array();
  33. while($row = $result->fetch_assoc()) {
  34. $users[$i] = new User();
  35. $users[$i]->setId($row[ 'id']);
  36. $users[$i]->setUsername($row[ 'username']);
  37. $users[$i]->setPassword($row[ 'password']);
  38. $users[$i]->setTelephone($row[ 'telephone']);
  39. $users[$i]->setRegisterTime($row[ 'register_time']);
  40. $users[$i]->setPopedom($row[ 'popedom']);
  41. $i++;
  42. }
  43. return $users;
  44. }
  45. public function login($username, $password)
  46. {
  47. $dataSource = new DataSource();
  48. $conn = $dataSource->getConn();
  49. $sql = "select * from t_user where username = '$username' and password = '$password'";
  50. $result = $conn->query($sql);
  51. if ($result->num_rows > 0) {
  52. return true;
  53. } else {
  54. return false;
  55. }
  56. }
  57. }
  58. ?>

说明:$result->fetch_array(MYSQLI_ASSOC) 就相当于 $result->fetch_assoc()

4、登录页面 - login.php


  
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>用户登录</title>
  5. <style type= "text/css">
  6. body {
  7. text-align: center;
  8. }
  9. table {
  10. margin: auto;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <h3>用户登录</h3>
  16. <form action= "doLogin.php" method= "POST">
  17. <table border= "1" cellspacing= "0" cellpadding= "5">
  18. <tr>
  19. <td>用户名</td><td>
  20. <input id= "username" type= "text" name= "username"></td>
  21. </tr>
  22. <tr>
  23. <td>密码</td>
  24. <td><input id= "password" type= "password" name= "password"></td>
  25. </tr>
  26. <tr>
  27. <td colspan= "2" align= "center">
  28. <input type= "submit" value= "登录" onclick= "return checkForm();">
  29. <input type= "reset" value= "重置">
  30. </td>
  31. </tr>
  32. </table>
  33. </form>
  34. </body>
  35. <script type= "text/javascript">
  36. function checkForm() {
  37. var username = document.getElementById( "username").value;
  38. var password = document.getElementById( "password").value;
  39. if (username == null || username == "") {
  40. alert( "用户名不能为空!");
  41. document.getElementById( "username").focus();
  42. return false;
  43. }
  44. if (password == null || password == "") {
  45. alert( "密码不能为空!");
  46. document.getElementById( "password").focus();
  47. return false;
  48. }
  49. return true;
  50. }
  51. </script>
  52. </html>

5、登录处理页面 - doLogin.php


  
  1. <?php
  2. require_once( "/net/hw/dao/UserDao.php");
  3. use \ net\ hw\ dao\ UserDao;
  4. $username = $_POST[ "username"];
  5. $password = $_POST[ "password"];
  6. $userDao = new UserDao();
  7. $success = $userDao->login($username, $password);
  8. if ($success) {
  9. $location = "success.php?username=$username";
  10. header( "location:$location");
  11. } else {
  12. $location = "failure.php?username=$username";
  13. header( "location:$location");
  14. }
  15. ?>

6、登录成功页面 - succes.php


  
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>登录成功</title>
  5. </head>
  6. <body>
  7. <h1>
  8. <?php
  9. $username=$_GET[ "username"];
  10. echo $username;
  11. ?>,登录成功</h1>
  12. </body>
  13. </html>

7、登录失败页面 - failure.php


  
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>登录失败</title>
  5. </head>
  6. <body>
  7. <h1>
  8. <?php
  9. $username=$_GET[ "username"];
  10. echo $username;
  11. ?>,登录失败</h1>
  12. </body>
  13. </html>

8、运行程序,访问http://localhost/login/login.php

9、显示单个用户页面  - showOneUser.php


  
  1. <?php
  2. require_once( "/net/hw/dao/UserDao.php");
  3. use \ net\ hw\ dao\ UserDao;
  4. $userDao = new UserDao();
  5. $id = 2;
  6. $user = $userDao->findUserById($id);
  7. echo $user . "<br>";
  8. ?>

10、运行程序,访问http://localhost/login/showOneUser.php

11、显示全部用户页面 - showAllUsers.php


  
  1. <?php
  2. require_once( "/net/hw/dao/UserDao.php");
  3. use \ net\ hw\ dao\ UserDao;
  4. $userDao = new UserDao();
  5. $users = $userDao->findAllUsers();
  6. foreach ($users as $user) {
  7. echo $user . "<br>";
  8. }

11、运行程序,访问http://localhost/login/showAllUsers.php

 


转载:https://blog.csdn.net/howard2005/article/details/79338834
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场