1.基本流程:
2.UML类图:
3.PHP代码:
3.1 index.php
<?php /** * Created by PhpStorm. * User: andy * Date: 16-11-25 * Time: 下午10:13 */ session_start(); $validate_username=isset($_SESSION[‘validate_username‘])?$_SESSION[‘validate_username‘]:‘‘; $validate_password=isset($_SESSION[‘validate_password‘])?$_SESSION[‘validate_password‘]:‘‘; ?> <html> <head> <meta charset="utf-8"/> <title>用户登录</title> </head> <body> <h1>用户登录</h1> <form action="login.php" method="post"> 用户名:<input type="text" name="username" value="" /><font color="red"> <?php echo $validate_username; ?> </font><br /><br /> 密 码:<input type="password" name="password" value="" /><font color="red"> <?php echo $validate_password; ?> </font><br /><br /> <input type="submit" value="提交" /> </form> </body> </html>
3.2 login.php
<?php /** * Created by PhpStorm. * User: andy * Date: 16-11-25 * Time: 下午10:20 */ session_start(); $username = $_POST[‘username‘]; $password = $_POST[‘password‘]; $user = new User($username, $password); //判断登录是否成功 try{ Validate::validateUser($user); //验证通过,登录成功 $_SESSION[‘username‘]=$username; header(‘location:main.php‘); }catch (MyException $me){ //验证失败 header(‘location:index.php‘); } /** * 自动加载类 * @param $class * @return string */ function __autoload($class) { $file = __DIR__ . ‘/‘ . strtolower($class) . ‘.php‘; if (is_file($file)) { include_once $file; } return ‘‘; }
3.3 myexception.php
<?php /** * Created by PhpStorm. * User: andy * Date: 16-11-25 * Time: 下午10:50 */ class MyException extends Exception { }
3.4 user.php
<?php /** * Created by PhpStorm. * User: andy * Date: 16-11-25 * Time: 下午10:29 */ class User { private $username = ‘‘; private $password = ‘‘; function __construct($username, $password) { $this->username = $username; $this->password = $password; } /** * 返回用户名 * @return string */ public function getUsername(): string { return $this->username; } /** * 返回密码 * @return string */ public function getPassword(): string { return $this->password; } }
3.5 validate.php
<?php /** * 验证类 * Created by PhpStorm. * User: andy * Date: 16-11-25 * Time: 下午10:34 */ class Validate { /** * 验证用户 * @param User $user * @throws MyException */ static function validateUser(User $user) { //print_r($user); $username = $user->getUsername(); $password = $user->getPassword(); unset($_SESSION[‘validate_username‘],$_SESSION[‘validate_password‘]); //验证用户名 try { self::validateUsername($username); }catch (MyException $me) { $_SESSION[‘validate_username‘]=$me->getMessage(); } //验证密码 try { self::validatePassword($password); }catch (MyException $me) { $_SESSION[‘validate_password‘]=$me->getMessage(); } if(isset($me)){ throw $me; } } /** * 验证用户名 * @param $username * @throws MyException */ static private function validateUsername($username) { $lem = strlen($username); if ($lem < 3) { //抛出异常 throw new MyException(‘用户名长度不能小于3位‘, E_USER_ERROR); } elseif ($lem > 8) { throw new MyException(‘用户名长度不能超过8位‘, E_USER_ERROR); } } /** * 验证密码 * @param $password * @throws MyException */ static private function validatePassword($password) { $lem = strlen($password); if ($lem < 3) { //抛出异常 throw new MyException(‘密码长度不能小于3位‘, E_USER_ERROR); } elseif ($lem > 8) { throw new MyException(‘密码长度不能超过8位‘, E_USER_ERROR); } } }
(完.)
时间: 2024-10-04 04:00:01