1.基本流程:php
2.UML類圖:html
3.PHP代碼:session
3.1 index.phpide
<?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.phppost
<?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.phpthis
<?php /** * Created by PhpStorm. * User: andy * Date: 16-11-25 * Time: 下午10:50 */ class MyException extends Exception { }
3.4 user.phpspa
<?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.phpcode
<?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); } } }
(完.)orm