詳細講解:使用tp3.2.3完成簡單的注冊登錄功能


 

 

  使用3.2.3進行了一個簡單不過的注冊登錄功能,界面介紹:

1、注冊:

 

 

2、登錄:

 

 

3、登錄成功后:

 

 

 

 

沒錯,就是簡單的讓你特別容易上手,上面運用到的知識有:

(1)自動驗證、自動完成

(2)session保存登錄名(目的,為了最后顯示歡迎界面)

 

目錄:

 

 實現步驟:

1、在Application\Home下創建上面的目錄文件

 

2、在數據中創建一個名為think_login的數據庫,此數據庫中存在一個think_user表,表里有以下字段

 

3、在Home\Config\config.php中進行連接數據庫

 

代碼:

<?php
return array(
    //'配置項'=>'配置值'
    //連接數據庫
    'DB_TYPE'      => 'mysql',//數據類型
    'DB_HOST'      => 'localhost',//服務器
    'DB_NAME'      => 'think_login',//數據庫名
    'DB_USER'      => 'root',//連接數據庫用戶名
    'DB_PWD'       => 'root',//密碼
    'DB_PREFIX'  => 'think_',//表前綴
    'DB_CHARSET' => 'utf8',//編碼
);

 

3、在loginController.class.php中定義兩個方法,一個是注冊功能(register),一個登錄功能(login):

(1)注冊功能:

 注冊界面的訪問地址為:index.php?c=login&a=register

 

控制器中:loginController.class.php中的register方法

 

代碼:

/**
     * 用戶注冊
     */
    public function register()
    {
        # 判斷提交方式 做不同處理
        if (IS_POST) {
            # 實例化User對象
            $user = D('user');

            # 通過create()調用對應的模型進行自動驗證 創建數據集
            if (!$data = $user->create()) {
                # 防止輸出中文亂碼
                header("Content-type: text/html; charset=utf-8");
                # 輸出對應的錯誤
                # 例如:用戶沒有輸入用戶名,就提示
                # 用戶名不能為空
                exit($user->getError());
            }
            
            //如果用戶輸入的都符合條件的話
            //插入數據庫
            if ($id = $user->add($data)) {

                $this->success('注冊成功', 'index.php?c=user&a=userlist', 1);
            } else {
                $this->error('注冊失敗');
            }
        } else {
            // 不帶任何參數 自動定位當前操作的模板文件
            $this->display();
        }
    }
View Code

 

 

對應模型:UserModel.class.php

注意:校驗中的變量名對應的是視圖中的name名,不是數據庫中的字段哦

 

代碼:

<?php
namespace Home\Model;
use Think\Model;
class UserModel extends Model {
    /**
     * 用戶注冊自動驗證(靜態驗證:$_validate)
     * self::EXISTS_VALIDATE 或者0 存在字段就驗證(默認)
     * self::MUST_VALIDATE 或者1 必須驗證
     * self::VALUE_VALIDATE或者2 值不為空的時候驗證
     */
    protected $_validate = array(
        array('user_name', 'require', '用戶名不能為空'),
        array('user_name', '', '該用戶已經被注冊', 0, 'unique', 1),
        array('user_pwd', 'require', '密碼不能為空'),
        # 用正則表達式驗證密碼, [必須包含字母+數字,且長度6~20字節]
        array('user_pwd', '/^(?=.*[A-Za-z])(?=.*[0-9])[A-Za-z0-9]{6,20}$/', '密碼格式不對:必須包含字母+數字,且長度6~20字節', 0),
        array('user_repwd', 'require', '確認密碼不能為空'),
        # 驗證兩次輸入密碼是否一致
        array('user_repwd', 'user_pwd', '兩次密碼不一致', 0, 'confirm'),
    );

    /**
     * 自動完成(用戶注冊成功之后)
     * @auther_start    小倉鼠
     * @auther_end      2018.04.15  小倉鼠
     * @return      無
     */
    protected $_auto = array(
        # 對password字段在新增和編輯的時候使md5函數處理
        array('user_pwd', 'md5', 3, 'function'),
    );
}

 

 

對應視圖中:register.html

 

代碼:

<html>
    <head>
        <meta charset="UTF-8">
        <title>新用戶注冊</title>
    </head>

    <body>
        <h2>注冊用戶界面</h2>
        <form method="POST" style="border:1px solid #000; text-align:center; width:300px">&nbsp;戶&nbsp;名:
            <input type="text" name="user_name" placeholder="請輸入用戶名">
            <br/>
            <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;碼:
            <input type="password" name="user_pwd" placeholder="請輸入密碼">
            <br/>
            <br>
            確認密碼:
            <input type="password" name="user_repwd" placeholder="請確認">
            <br/>
            <br/>
            <input type="submit" value="注冊">
            <a href="index.php?c=login&a=login">已經注冊,直接登錄</a>
        </form>
    </body>
</html>

 

 

(2)登錄功能: 

登錄界面的訪問地址為:index.php?c=login&a=login

 

控制器中:loginController.class.php中的login方法

 

代碼:

 /**
     * 用戶登錄
     */
    public function login()
    {
        # 清空session之前保留的數據
        session('uname',null);

        # 判斷提交方式
        if (IS_POST) {
            # 實例化Login對象
            $login = D('login');
            
            # 通過create()調用對應的模型進行自動驗證 創建數據集
            # $data = 獲取用戶輸入的信息
            if (!$data = $login->create()) {
                # 防止輸出中文亂碼
                header("Content-type: text/html; charset=utf-8");
                # 輸出對應的錯誤
                # 例如:用戶沒有輸入用戶名,就提示
                # 用戶名不能為空
                exit($login->getError());
            }

            // 組合查詢條件
            $where = array();
            # $where['user_name']:數據庫中的user_name字段
            # $data['user_name':與此模型同名視圖下的name名
            $where['user_name'] = $data['user_name'];
            $where['user_repwd'] = $data['user_repwd'];
            # 相當於:
            # select * from think_user where user_name=用戶輸入的user_name and user_repwd=用戶輸入的密碼;
            # result輸出為一個二維數組
            $result = $login->where($where)->select();

            if ($result) {  
                # 保存session
                # 將$result['user_name']值賦值給session,名字叫uname               
                session('uname', $result[0]['user_name']);
                         
                $this->success('登錄成功,正跳轉至用戶列表...','index.php?c=user&a=userlist', 1);
            } else {
                $this->error('登錄失敗,用戶名或密碼不正確!');
            }
        } else {
            $this->display();
        }
    }
View Code

 

 

 對應的模型:loginModel.class.php

 

代碼:

<?php
namespace Home\Model;
use Think\Model;
class LoginModel extends Model{
    //重新定義表
    protected $tableName = 'user';

    /**
     * 自動驗證(靜態驗證用:$_validate)
     * 手冊中:模型->自動驗證、自動完成
     * self::EXISTS_VALIDATE 或者0 存在字段就驗證(默認)
     * self::MUST_VALIDATE 或者1 必須驗證
     * self::VALUE_VALIDATE或者2 值不為空的時候驗證
     */
    protected $_validate = array(
        array('user_name', 'require', '用戶名不能為空'),
        array('user_pwd', 'require', '密碼不能為空'),
        array('user_repwd', 'require', '確認密碼不能為空'),
        # 驗證兩次輸入密碼是否一致
        array('user_repwd', 'user_pwd', '兩次密碼不一致', 0, 'confirm'),
    );

    /**
     * 自動完成
     * 靜態方式:在模型類里面通過$_auto屬性定義處理規則。
     */
    protected $_auto = array(
        /* 登錄時自動完成 */
        # 對password字段使用md5函數處理
        array('password', 'md5', 3, 'function'),
    );
}

 

 

 對應視圖中:login.html

 

代碼:

<html>
        <head>
            <meta charset="UTF-8">
            <title>登錄</title>
        </head>
    
        <body>
            <h2>登錄用戶界面</h2>
            <form method="POST" style="border:1px solid #000; text-align:center; width:300px">&nbsp;戶&nbsp;名:
                <input type="text" name="user_name" placeholder="請輸入登錄用戶名">
                <br/>
                <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;碼:
                <input type="password" name="user_pwd" placeholder="請輸入登錄密碼">
                <br/>
                <br>
                確認密碼:
                <input type="password" name="user_repwd" placeholder="請確認登錄密碼">
                <br/>
                <br/>
                <input type="submit" value="登錄">
                <a href="index.php?c=login&a=register">還沒有注冊,去注冊</a>
            </form>
        </body>
    </html>

 

(3)登錄成功后控制器:UserController.class.php

 

 

以上就是整個簡單的注冊登錄全部代碼了,在實現功能中,當你對某個方法的最終顯示效果不確定的時候,記得去打印出來看看,有必要的話必須要查看手冊,手冊上寫的一清二楚,所以不要靠猜,只要你不確定這個方法的實際操作,一定要看手冊哦,答應我!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM