面向对象编程(OOP)PHP登录教程
需求分析:
1. 用户应该能够输入他们的用户名和密码来登录应用程序。
2. 应用程序应该验证用户提供的凭据是否正确,并在登录成功时显示“登录成功!”消息。
3. 如果用户提供的凭据不正确,则应用程序应该显示“用户名或密码错误。”消息并允许用户重试。
4. 应用程序应该与MySQL数据库交互,以验证用户凭据并检索其他必要的数据。
代码运行图:
登录数据库和表设计:
1. 创建一个名为“login”的数据库。
2. 在“login”数据库中创建一个名为“users”的表,该表将包含用户名和密码列。
-- ---------------------------- -- Table structure for users -- ---------------------------- DROP TABLE IF EXISTS `users`; CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) NOT NULL, `password` varchar(32) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of users -- ---------------------------- INSERT INTO `users` VALUES ('1', 'google', '202cb962ac59075b964b07152d234b70');
代码编写思路:
创建两个类:一个用于表示用户,另一个用于处理登录。创建了一个包含登录表单和处理用户输入的PHP文件。当用户提交表单时,我们将创建一个新的Login对象并使用其authenticate方法验证用户凭据。如果验证成功,我们将输出“登录成功!”,否则我们将输出“用户名或者密码错误.”。
完整代码:
index.php 文件
<?php require_once('Login.php'); // 引入Login类 if ($_SERVER['REQUEST_METHOD'] == 'POST') { // 如果是POST请求 $username = $_POST['username']; // 获取用户名 $password = $_POST['password']; // 获取密码 $password = md5($password); $login = new Login(); // 创建一个新的Login对象 if ($login->authenticate($username, $password)) { // 如果验证成功 $message = '登录成功!'; // 输出登录成功消息 //header("Location:http://baidu.com"); //登录成功后跳转 } else { $message = '用户名或密码错误。'; // 输出错误消息 } } ?> <!DOCTYPE html> <html> <head> <title>Login Page</title> <style> body { background-color: #f2f2f2; font-family: Arial, sans-serif; } .container { margin: 100px auto; width: 400px; background-color: #fff; padding: 20px; border-radius: 5px; box-shadow: 0px 0px 10px #ccc; } h1 { text-align: center; margin-bottom: 30px; } input[type="text"], input[type="password"] { width: 100%; padding: 10px; margin-bottom: 20px; border: none; border-radius: 5px; box-shadow: 0px 0px 5px #ccc; } input[type="submit"] { background-color: #4CAF50; color: #fff; padding: 10px; border: none; border-radius: 5px; cursor: pointer; width: 100%; } input[type="submit"]:hover { background-color: #3e8e41; } </style> </head> <body> <div class="container"> <h1>Login</h1> <h3 style="color: red;text-align: center;"><?php echo $message;?></h3> <form action="index.php" method="post"> <label for="username">用户名:</label> <input type="text" id="username" name="username" required> <label for="password">密码:</label> <input type="password" id="password" name="password" required> <input type="submit" value="登录"> </form> </div> </body> </html>
Login.php 文件
<? // 定义一个User类 class User { // 定义私有属性$username和$password private $username; private $password; // 定义构造函数,初始化$username和$password public function __construct($username, $password) { $this->username = $username; $this->password = $password; } // 定义获取$username的方法 public function getUsername() { return $this->username; } // 定义获取$password的方法 public function getPassword() { return $this->password; } } // 定义一个Database类 class Database { // 定义私有属性$host、$username、$password、$database和$connection private $host; private $username; private $password; private $database; private $connection; // 定义构造函数,初始化$host、$username、$password和$database public function __construct($host, $username, $password, $database) { $this->host = $host; $this->username = $username; $this->password = $password; $this->database = $database; } // 定义连接到MySQL数据库的方法 public function connect() { // 连接到MySQL数据库 $this->connection = mysqli_connect($this->host, $this->username, $this->password, $this->database); // 如果连接失败,则输出错误信息并停止脚本执行 if (!$this->connection) { die('连接失败:' . mysqli_connect_error()); } } // 定义获取数据库连接的方法 public function getConnection() { return $this->connection; } } // 定义一个Login类 class Login { // 定义私有属性$database private $database; // 定义构造函数,创建一个新的Database对象并连接到MySQL数据库 public function __construct() { $this->database = new Database('localhost', 'root', 'root', 'login'); $this->database->connect(); } // 定义验证用户身份的方法 public function authenticate($username, $password) { // 获取数据库连接 $connection = $this->database->getConnection(); // 构建查询语句 $query = "SELECT * FROM users WHERE username='{$username}' AND password='{$password}'"; // 执行查询 $result = mysqli_query($connection, $query); // 如果查询返回一行结果,则返回true,否则返回false if (mysqli_num_rows($result) == 1) { return true; } else { return false; } } }