目录机构:
然后我的改动代码:
MysqlDB.class.php
1 <?php 2 3 /** 4 * Created by PhpStorm. 5 * User: Interact 6 * Date: 2017/8/19 7 * Time: 19:32 8 */ 9 class MysqlDB { 10 private static $link; 11 public $host; 12 public $port; 13 public $username; 14 public $passsword; 15 public $charset; 16 //数据库连接对象 17 public $dbname;//防止未接破坏这个连接对象,这个link就是MysqlDB 对象 18 private $resourc; 19 20 /** 21 * @param $config,你的配置数组 22 * @return 获取数据库连接对象$link,同时作为返回值 23 */ 24 private function __construct($config) { 25 $this->host = isset($config['host']) ? $config['host'] : 'localhost'; 26 $this->port = isset($config['port']) ? $config['port'] : '3306'; 27 $this->username = isset($config['username']) ? $config['username'] : 'root'; 28 $this->password = isset($config['password']) ? $config['password'] : ''; 29 $this->charset = isset($config['charset']) ? $config['charset'] : 'utf8'; 30 $this->dbname = isset($config['dbname']) ? $config['dbname'] : ''; 31 //连接数据库 32 $this->connect(); 33 //设定连接编码 34 //$this->setCharset($this->charset);//这个执行不了,可能新的php有了更改 35 //选定数据库 36 $this->selectDb($this->dbname); 37 } 38 39 //构造函数,禁止new,这样可以用工厂函数来创造类 40 public function connect() { 41 $this->resourc = mysqli_connect("$this->host", 42 "$this->username", "$this->password") or die("连接数据库失败!"); 43 } 44 45 //禁止克隆 46 public function selectDb($dbname) { 47 mysqli_select_db($this->resourc, $dbname); 48 } 49 50 public static function getInstance($config) { 51 if (!isset(self::$link)) { 52 self::$link = new self($config); 53 //或者是 self::$link=$this->__construct($config); 54 } 55 56 return self::$link; 57 } 58 59 /** 60 * 功能:执行select语句,返回2维数组 61 * 参数:$sql 字符串类型 select语句 62 */ 63 public function getAll($sql) { 64 $result = $this->query($sql); 65 $arr = array(); //空数组 66 while ($rec = mysqli_fetch_assoc($result)) { 67 $arr[] = $rec;//这样就形成二维数组 68 } 69 70 return $arr; 71 } 72 73 /** 74 * 功能:执行最基本(任何)sql语句 75 * 返回:如果失败直接结束,如果成功,返回执行结果 76 */ 77 public function query($sql) { 78 if (!$result = mysqli_query($this->resourc, $sql)) { 79 echo("<br />执行失败。"); 80 echo "<br />失败的sql语句为:".$sql; 81 echo "<br />出错信息为:".mysqli_error($this->resourc); 82 echo "<br />错误代号为:".mysqli_errno($this->resourc); 83 die(); 84 } 85 86 return $result; 87 } 88 89 public function getRow($sql) { 90 $result = $this->query($sql); 91 //$rec = array(); 92 if ($rec2 = mysqli_fetch_assoc($result)) {//返回下标为字段名的数组 93 //如果fetch出来有数据(也就是取得了一行数据),结果自然是数组 94 return $rec2; 95 } 96 97 return false; 98 } 99 100 //返回一行数据(作为一维数组) 101 102 public function getOne($sql) { 103 $result = $this->query($sql); 104 $rec = mysqli_fetch_row($result);//返回下标为数字的数组,且下标一定是0,1,2, 3..... 105 //如果没有数据,返回false 106 if ($result === false) { 107 return false; 108 } 109 110 return $rec[0]; //该数组的第一项。 111 112 } 113 //返回一个数据(select语句的第一行第一列) 114 //比如常见的:select count(*) as c from XXX where ... 115 116 private function __clone() { 117 } 118 /** 119 * 转义用户数据,防止SQL注入 120 * @param $data string 带转换的字符串 121 * @return string 122 * 转换后的字符串 123 */ 124 public function escapeString($data){ 125 return mysqli_real_escape_string(self::$link,$data); 126 } 127 }
AdminModel.class.php
1 <?php 2 /** 3 * Created by PhpStorm. 4 * User: Interact 5 * Date: 2017/8/21 6 * Time: 8:39 7 */ 8 class AdminModel extends Model{ 9 /** 10 * @param $admin_name 11 * @param $admin_pass 12 *后台登录验证函数 13 * @return bool 14 */ 15 public function check($admin_name, $admin_pass) { 16 $admin_name=$this->_dao->escapeString($admin_name); 17 $admin_pass=$this->_dao->escapeString($admin_pass); 18 $sql = "SELECT * FROM `admin` WHERE admin_name='$admin_name' and admin_pass=md5('$admin_pass')"; 19 $row = $this->_dao->getRow($sql); 20 21 return (bool) $row; 22 } 23 }
AdminC.controller.class.php
1 <?php 2 /** 3 * Created by PhpStorm. 4 * User: Interact 5 * Date: 2017/8/20 6 * Time: 14:22 7 */ 8 class AdminC extends Controller{ 9 10 11 public function login(){ 12 // require 13 require APPLICATION_PATH.'back/view/login.html'; 14 } 15 /** 16 * 验证管理员是否合法 17 */ 18 public function check() { 19 // echo "MC天佑MC天佑MC天佑"; 20 // echo $_REQUEST['username']; 21 // 获得表单数据 22 /*echo $_REQUEST['username']; 23 echo ' '; 24 echo $_REQUEST['password'];*/ 25 $admin_name = $_REQUEST['username']; 26 $admin_pass = $_REQUEST['password']; 27 $admin_name=addslashes($admin_name); 28 $admin_pass=addslashes($admin_pass); 29 30 31 32 //从数据库中验证管理员信息是否存在合法 33 $m_admin = Factory::M('AdminModel'); 34 if ($m_admin->check($admin_name, $admin_pass)) { 35 // //验证通过,合法 36 // echo '合法,直接跳转到后台首页'; 37 session_start(); 38 // $_SESSION['is_login']='yes'; 39 new SessionDB(); 40 $this->_jump('index.php?p=back&c=BACkC&a=index'); 41 } else { 42 // 非法 43 // echo '非法, 提示,跳转到后台登陆页面index.php?p=back&c=Admin&a=login'; 44 $this->_jump('index.php?p=back&c=AdminC&a=login','用户名或密码错误'); 45 } 46 // 47 } 48 }
结果展示: