PHP使用OOP注冊(cè)登錄

一、頁(yè)面關(guān)系結(jié)構(gòu)圖

  • Index.php是入口主文件,根據(jù)自己的情況需要,載入start.inc.php(初始頁(yè)面)、register.inc.php(注冊(cè)頁(yè)面)、login.inc.php(登錄頁(yè)面)、member.inc.php(個(gè)人中心頁(yè)面)。
templte.png

1、index.php

# 類的自動(dòng)加載
<?php
function __autoload($_classname) {
    require $_classname.".class.php";
}
if (isset($_GET["index"])) {
    $_main = new Main($_GET["index"]);
} else {
    $_main = new Main();
}
$_main ->_run();
?>

2、start.inc.php

<h2>歡迎光臨注冊(cè)登錄</h2>
<div class="start">
    <a href="index.php?index=login">登錄</a>
    <a href="index.php?index=register">注冊(cè)</a>
</div>
index.png

3、register.inc.php

<h2>歡迎注冊(cè)</h2>
<div class="register">
    <form method="post" action="">
        <p>用 戶 名:<input type="text" name="username"></p>
        <p>密  碼 :<input type="password" name="password"></p>
        <p>密碼確認(rèn):<input type="password" name="notpassword"></p>
        <p>電子郵件:<input type="text" name="email"></p>
        <p><input type="submit" name="send" value="注冊(cè)"></p>
        <p>[<a href="?">返回上一層]</a></p>
    </form>
</div>
register.png

4、login.inc.php

<h2>歡迎登錄</h2>
<div class="login">
    <form method="post" action="">
        <p>用戶名:<input type="text" name="username"></p>
        <p>密 碼:<input type="password" name="password"></p>
        <p><input type="submit" name="send" value="登錄"></p>
        <p>[<a href="?">返回上一層]</a></p>
    </form>
</div>
login.png

5、mumber.inc.php

<h2>歡迎回來(lái)</h2>
<div class="member">
    <p>歡迎回來(lái),[<?php if (isset($_COOKIE["user"])) echo $_COOKIE["user"]?>]</p>
    <p>[<a href="?">返回首頁(yè)</a>]</p>
</div>
member.png

二、業(yè)務(wù)關(guān)系結(jié)構(gòu)圖

  • 在業(yè)務(wù)邏輯上,Main.class.php作為主入口調(diào)用各個(gè)模塊處理的功能,比如:User.class.php(登錄注冊(cè)處理父類)、Register.class.php(注冊(cè)類)、Login.class.php(登錄類)、Tool.class.php(工具類)。
templte1.png

1、Main.class.php

//主類,控制頁(yè)面載入,處理數(shù)據(jù)
class Main
{
    private $_index;
    private $_send;
    //構(gòu)造方法,用來(lái)初始化數(shù)據(jù)
    public function __construct($_index="")
    {
        $this->_index = $_index;
        if (isset($_POST["send"])) {
            $this->_send = $_POST["send"];
        }
    }
    //總管
    public function _run() {
        //調(diào)用注冊(cè)登錄方法
        $this->_send();
        //載入界面
        include $this->_ui();
    }

    //創(chuàng)建一個(gè)載入界面的方法
    //這個(gè)方法,我想得到login.inc.php這個(gè)字符串
    private function _ui()
    {
        if (empty($this->_index)|| !file_exists($this->_index.".inc.php")) {
            $this->_index = "start";
        }
        return $this->_index.".inc.php";
    }
    //創(chuàng)建一個(gè)方法來(lái)接收登錄和注冊(cè)發(fā)送的操作
    private function _send()
    {
        switch ($this->_send) {
            case "注冊(cè)":
                $this->_exec(new Register($_POST["username"],$_POST["password"],$_POST["notpassword"],$_POST["email"]));
                break;
            case "登錄":
                $this->_exec(new Login($_POST["username"],$_POST["password"]));
                break;
        }
    }

    //創(chuàng)建一個(gè)執(zhí)行的方法,里面?zhèn)饕粋€(gè)參數(shù),是Reg或者Login類的對(duì)象引用
    private function _exec($_class)
    {
        if ($_class->_check()) {
            $_class->_query();
        } else {
            Tool::_alertBack("字段不能為空");
        }
    }

2、User.class.php

//這個(gè)用戶類,規(guī)范子類的字段和方法
//這是個(gè)抽象類,不可以實(shí)例化只能是子類繼承
abstract class User
{
    //成員字段
    protected $_username;
    protected $_password;
    protected $_notpassword;
    protected $_email;
    //一個(gè)方法,登錄和注冊(cè)
    //如果你點(diǎn)了登錄,就執(zhí)行這個(gè)方法登錄
    //如果你點(diǎn)了注冊(cè),就執(zhí)行這個(gè)方法注冊(cè)
    abstract function _query();
    //驗(yàn)證
    abstract function _check();
}

3、Register.class.php

class Register extends User
{
    //寫一構(gòu)造方法來(lái)接收表單的值
    public function __construct($_userName,$_passWord,$_notPassWord,$_eMail)
    {
        $this->_username = $_userName;
        $this->_password = $_passWord;
        $this->_notpassword = $_notPassWord;
        $this->_email = $_eMail;
    }
    //將信息注冊(cè)到xml里
    public function _query()
    {
        //xml字符串
        $_xml = <<<_xml
<?xml version="1.0" encoding="utf-8"?>
<user>
    <username>$this->_username</username>
    <password>$this->_password</password>
    <email>$this->_email</email>
</user>
_xml;
        //創(chuàng)建simplexml類
        $_sex = new SimpleXMLElement($_xml);
        //生成xml
        $_sex->asXML("user.xml");
        //跳轉(zhuǎn)到login.inc.php頁(yè)面
        Tool::_alertLocation("恭喜您,注冊(cè)成功","?index=login");
    }
    # 注冊(cè)驗(yàn)證,只是限定不能為空
    public function _check()
    {
        if (empty($this->_username)||
            empty($this->_password)||
            empty($this->_notpassword)||
            empty($this->_email) ){
            return false;
        }
        return true;
    }
}

4、Login.class.php

class Login extends User
{
    public function __construct($_username,$_password)
    {
        $this->_username = $_username;
        $this->_password = $_password;
    }

    //從xml里讀出信息
    public function _query()
    {
        //載入xml文件
        $_sex = simplexml_load_file("user.xml");
        if ($this->_username == $_sex->username && $this->_password == $_sex->password) {
           //生成一個(gè)cookies
           setcookie("user",$this->_username);
           Tool::_alertLocation($this->_username."歡迎回來(lái)","?index=member");
        } else {
           Tool::_alertBack("登錄失敗");
        }
    }
    # 登錄驗(yàn)證,只是驗(yàn)證不能為空
    public function _check()
    {
        if (empty($this->_username) || empty($this->_password)) {
            return false;
        }
        return true;
    }
}

5、Tool.class.php

//輔助工具類,里面存放的都是靜態(tài)方法,直接調(diào)用,無(wú)需實(shí)例化
class Tool
{
    //彈出一個(gè)信息,然后跳轉(zhuǎn)到指定的頁(yè)面
    static public function _alertLocation($_info,$_url)
    {
        echo "<script>alert('$_info');location.href='$_url';</script>";
        exit();
    }
    //彈窗返回之前
    static public function _alertBack($_info)
    {
        echo "<script>alert('$_info');history.back()</script>";
        exit();
    }
}

總結(jié)

本程序使用到了OOP和XML技術(shù)來(lái)注冊(cè)、登錄。使用到了類和面向?qū)ο髣?chuàng)建程序的思維和方法。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容