PHP基础-如何实现一个简单的MVC方式登录系统

1.数据库

create database test//可以使用phpMyAdmin创建,格式为uft-general-ci。

create table user(
    id int(4) AUTO_INCREMENT PRIMARY KEY,
    username varchar(20) not null,
    password varchar(20)
    )

2.注册页面(18.php)

<html>
<head>
    <title>虾米大王使用MVC框架实现的用户登录系统</title>
</head>
<body>
    <span>虾米大王使用MVC框架实现的用户登录系统</span>
    <form id="form1" name="form1" method="post" action="20.php?act=add_user">
        <table>
            <caption>注册用户</caption>
            <tr>
                <td>用户名:</td>
                <td><input type="text" name="user_name" id="user_name"></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><input type="password" name="password" id="password"></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><input type="submit" name="button1" value="注册"></td>
            </tr>
        </table>
    </form>
</body>
</html>

3.登录页面(19.php)

<html>
<head>
    <title>虾米大王使用MVC框架实现的用户登录系统</title>
</head>
<body>
<span>虾米大王使用MVC框架实现的用户登录系统</span>
<form id="form1" name="form1" method="post" action="20.php?act=login">
    <table>
        <caption>用户登录</caption>
        <tr>
            <td>用户名:</td>
            <td><input type="text" name="user_name" id="user_name"></td>
        </tr>
        <tr>
            <td>密码:</td>
            <td><input type="password" name="password" id="password"></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="submit" name="button1" value="登录"></td>
        </tr>
    </table>
</form>
</body>
</html>

4.控制器controler(20.php)

<?php
/**
 * Created by PhpStorm.
 */

require_once ('17.php');  //引入view(视图层)

$user = new user();
$user_name =@ $_POST['user_name'];
$password =@ $_POST['password'];

if($_GET['act'] == 'login')  //GET方式获取
{
    if($user->login($user_name,$password))
    {
        echo "欢迎 ".$_SESSION['user_name']."<br>";
        echo "登录成功!";
        echo "<a href='20.php?act=logout'>注销</a>";
    }
    else
    {
        echo "登录失败!";
    }
}
elseif($_REQUEST['act'] == 'logout')  // REQUEST方式包含GET和POST的全部数据,
{
    $user->logout();
    echo "注销成功!";

}
elseif($_GET['act'] == 'add_user')
{
    $result = $user->add_user($user_name,$password);
    if($result == -1)
    {
        echo "该用户已存在!<br>";
    }
    elseif($result == 1)
    {
        echo "注册成功!";
    }
    else
    {
        echo "注册失败!";
    }
}
else
{
    echo "参数错误!";
}

5.视图层view(17.php)

<?php
/**
 * Created by PhpStorm.
 */

require_once ('16.php');  //引入model(模型层)
session_start();

class user
{
    var $db;

    function __construct()
    {
        $this->db = new DB("mysql:dbname=test;host=localhost",'root','');
    }

    function login($user_name,$password)
    {
        $user = $this->db->get_one("select * from user where username=? and password =?",array($user_name,$password));
        if($user)
        {
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['user_name'] = $user['username'];
            return true;
        }
        else
        {
            return false;
        }
    }

    function logout()
    {
        unset($_SESSION['user_id']);
        unset($_SESSION['user_name']);
        session_destroy();
        return 1;
    }

    function add_user($user_name,$password)
    {
        $bool = $this->db->get_col("select count(1) from user where username=?",array($user_name));
        if($bool)
        {
            return -1;
        }
        $result = $this->db->insert('user',array('username'=>$user_name,'password'=>$password));
        if($result)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }


}



6.模型层model(16.php)

<?php
/**
 * Created by PhpStorm.
 */

class DB extends PDO  // 继承PDO数据库类
{
    var $sth;

    function execute($sql,$values = array())
    {
        $this->sth = $this->prepare($sql);
        return $this->sth->execute($values);
    }

    function get_all($sql,$values = array())
    {
        $this->execute($sql,$values);
        return $this->sth->fetchAll();
    }

    function get_one($sql,$values = array())
    {
        $this->execute($sql,$values);
        return $this->sth->fetch();
    }

    function get_col($sql,$values = array(),$column_number = 0)
    {
        $this->execute($sql,$values);
        return $this->sth->fetchColumn($column_number);
    }

    function insert($table,$data)
    {
        $fields = array_keys($data);
        $marks = array_fill(0,count($fields),'?');
        $sql = "insert into $table (`".implode('`,`',$fields)."`) values (".implode(",",$marks)." )";
        return $this->execute($sql,array_values($data));

    }

    function update($table,$data,$where)
    {
        $values = $bits = $wheres = array();
        foreach ($data as $k => $v)
        {
            $bits[] = "`$k` = ?";
            $values[] = $v;
        }

        if(is_array($where))
        {
            foreach ($where as $c => $v)
            {
                $wheres[] = "$c = ?";
                $values[] = $v;
            }
        }
        else
        {
            return false;
        }

        $sql = "update $table set ".implode(',',$bits).' where '.implode(' and ',$wheres);
        return $this->execute($sql,$values);

    }

    function dalete($table,$field,$where)
    {
        if(empty($where))
        {
            return false;
        }

        $this->sth = $this->prepare("delete from $table where $field = ?");
        if(is_array($where))
        {
            foreach ($where as $key => $val)
            {
                $this->sth->execute(array($val));

            }
        }
        else
        {
            $this->sth->execute(array($where));
        }

    }

    function table2sql($table)
    {
        $sql = array();
        $sql[] = "drop table if exists `{$table}`;\n";
        $create_table = $this->get_one('show create table'.$table);
        $sql[] = $create_table[1].";\n\n";
        return implode('',$sql);
    }

    function data2sql($table)
    {
        $sql = array();
        $sql[] = "drop table if exists `{$table}`;\n";
        $create_table = $this->get_one('show create table'.$table);
        $sql[] = $create_table[1].";\n\n";
        $rows = $this->get_all("select * from $table");
        $col_count = $this->sth->columnCount();
        foreach($rows as $row)
        {
            $sql[] = "insert into $table values(";
            $comma = '';
            for($i=0; $i<$col_count; $i++)
            {
                if(!isset($row[$i]))
                {
                    $sql[] = $comma." NULL";
                }
                else
                {
                    $sql[] = $comma."'".$row[$i]."''";
                }
                $comma = ',';
            }
            $sql[] = ");\n";
        }
        $sql[] = "\n";
        return implode('',$sql);
    }

    function dump_sql()
    {
        $sql = array();
        foreach ($this->query('show tables') as $row)
        {
            $sql[] = $this->data2sql($row[0]);
        }
        return implode('',$sql);
    }
}

最后的模型层,get_one,get_col,exceute,insert函数都正常可以使用,其他函数没有亲自测试,尚不知会不会有bug。

猜你喜欢

转载自blog.csdn.net/modern358/article/details/89765709