標籤:== line 包含 pad 資料庫名 logo extends sans 模組
6月6日,晴天。“熟梅天氣豆生蛾。一見榴花感慨多。
芒種積陰凝雨潤,菖蒲修剪莫蹉跎。
”
九、後台編寫-續-1
1、改動admin/Tpl/Login/index.html
自己改動logo,使用者名稱、密碼都改為中文。文字框分別加入name="username"。name="password"。
重點改動<form action="index.html">。改為<form action="__URL__/login" method="post">
解釋: __URL__是當前模組的URL地址。
在PHP裡:__URL__ 當前模組的URL地址
在模版裡:__URL__ 會替換成當前模組的URL地址(不含網域名稱)
action="__URL__/login" 是提交到當前模組的login方法下
2、編寫Login模組的login方法
<?phpclass LoginAction extends Action {function index() {// 配置頁面顯示內容$this->assign ( ‘title‘, ‘後台管理系統‘ );$this->display ();}//使用者登入頁面function login(){header("Content-Type:text/html; charset=utf-8");//首先檢查驗證碼是否正確(驗證碼存在Session中)if($_SESSION[‘verify‘] != md5($_POST[‘verify‘])){$this->error(‘驗證碼不對‘);}$user=M(‘User‘);//參數的User必須首字母大寫,否則自己主動驗證功能失效!
$username=$_POST[‘username‘];$password=md5($_POST[‘password‘]);if(!$this->checklen($username)){ $this->error(‘使用者名稱長度必須在5~15個字元之間‘); }//尋找輸入的使用者名稱是否存在 if($user->where("username =‘$username‘ AND password = ‘$password‘")->find()){session(username,$username);$url=U(‘/Index/index/username/‘.$username);redirect($url,0, ‘跳轉中...‘); }else{$this->error(‘使用者名稱或密碼錯誤‘); }}function checklen($data){if(strlen($data)>15 || strlen($data)<5){return false;}return true;}}
當中,$user = M(‘User‘);表示執行個體化User模型,事實上等效於:$User = new Model(‘User‘);表示操作think_user表。
在未定義不論什麼模型的時候,我們能夠使用M方法執行個體化一個模型類來進行操作。這樣的方法最簡單高效,由於不須要定義不論什麼的模型類,所以支援跨項目調用。缺點也是由於沒有自己定義的模型類,因此無法寫入相關的商務邏輯,僅僅能完畢主要的CURD操作。
M方法的調用格式:M(‘[基本模型名:]模型名‘,‘資料表首碼‘,‘資料庫連接資訊‘)
M方法有三個參數。第一個參數是模型名稱(能夠包含基本模型類和資料庫),第二個參數用於設定資料表的首碼(留空則取當前項目配置的表首碼),第三個參數用於設定當前使用的資料庫連接資訊(留空則取當前項目配置的資料庫連接資訊)
第二個參數留空預設表首碼think_。 第三個參數留空,則須要配置admin\Conf\config.php
<?php$arr1=array(//‘配置項‘=>‘配置值‘‘URL_MODEL‘=>1,//path-info 模式);$arr2=include ‘./config.inc.php‘;return array_merge($arr1,$arr2);?
>
config.inc.php配置
<?php return array(‘DB_TYPE‘=>‘mysql‘,‘DB_HOST‘=>‘localhost‘,‘DB_NAME‘=>‘tp‘,//資料庫名‘DB_USER‘=>‘root‘,//username‘DB_PWD‘=>‘root‘,//password‘DB_PORT‘=>‘3306‘,‘DB_PREFIX‘=>‘think_‘,//資料庫表名首碼);?>
M(‘User‘);表示執行個體化User模型。模型名字是User,該模型就是資料庫表think_user
drop table if exists think_user;/*==============================================================*//* Table: think_user *//*==============================================================*/create table think_user( id int(11) not null auto_increment, username char(15) not null, password char(40) not null, createtime int(11) not null, ip char(16) not null, primary key (id));
代碼釋義:
ThinkPHP內建了很靈活的查詢方法,能夠高速的進行資料查詢操作,查詢條件能夠用於CURD等不論什麼操作,作為where方法的參數傳入就可以: if($user->where("username =‘$username‘ AND password = ‘$password‘")->find())。
最後產生的SQL語句是SELECT * FROM think_user WHERE `username`=‘admin‘ and `password`=‘admin‘。
Session賦值比較簡單。直接使用: session(username,$username);相當於:$_SESSION[‘username‘] =$username;
$url=U(‘/Index/index/username/‘.$username);中username是傳遞的參數名,$username是參數值。
redirect($url,0, ‘跳轉中...‘);Action類的redirect方法能夠實現頁面的重新導向功能。使用方法是停留0秒後跳轉到Index模組的index操作,而且顯示頁面跳轉中字樣。重新導向後會改變當前的URL地址。
3、 改動admin/Lib/Action/IndexAction.class.php,例如以下:
<?php// 本類由系統自己主動產生,僅供測試用途class IndexAction extends Action {public function index() {if (session ( ‘?username‘ )) {$this->display();} else {$this->error ( ‘親,您好,請先登入!‘, U ( ‘/Login/index/‘ ) );}}}
代碼釋義:
this->assign(‘name‘,$value); //在 Action 類裡面使用 assign方法對模板變數賦值,不管何種變數類型都統一使用 assign 賦值。
$this->display() // 輸出預設模版檔案
模板預設輸出是指輸出指令 display 方法中不帶不論什麼參數,系統預設輸出相應模板,即admin/Tpl/Index/index.html。
4、 用下載的後端模板裡面檔案的index.html放在admin\Tpl\Index\index.html裡面,改動index.html檔案中的相關資源路徑。如:<link rel="stylesheet" href="__PUBLIC__/Css/admin/reset.css" type="text/css" media="screen" />等。還有public\Css\admin\style.css裡面的如:background: #f0f0f0 url(‘../../Images/admin/bg-body.gif‘)
5、訪問:http://localhost/MyFirstThinkPHP_1/admin.php ,呵呵。出來了!須要在建好的資料庫表think_user中填寫username為admin,password為admin(21232f297a57a5a743894a0e4a801fc3)。
第四天 ThinkPHP手把手高速拼接網站(四)