此文件時入口文件index.php
<?php
//定義一下ThinkPHP框架存放的路徑
define('THINK_PATH','./ThinkPHP/');
//定義當前的項目的名稱,此處的項目可理解為模塊home理解為前台部分
define('APP_NAME','protal');
//定義項目的路徑
define('APP_PATH','./protal/');
define('APP_DEBUG', true);
require THINK_PATH.'ThinkPHP.php';
conf/config.php
<?php //包含定義配置數據庫連接的配置文件 $dbConf=include './config.inc.php'; //定義項目本身常規配置 $Conf=array( //'配置項'=>'配置值' 'URL_MODEL'=>2,//2表示是URL重寫模式 ); return array_merge($dbConf,$Conf); ?>
與入口文件同級的有一個配置文件config.inc.php
<?php return array( //'配置項'=>'配置值' 'DB_TYPE'=>'mysql', 'DB_HOST'=>'localhost', //數據庫名 'DB_NAME'=>'think', //數據庫用戶 'DB_USER'=>'root', //數據庫密碼 'DB_PWD'=>'', //數據庫端口 'DB_PORT'=>'3306', //表前綴 'DB_PREFIX'=>'t_', ) ?>
控制器IndexAction.class.php
<?php
// 本類由系統自動生成,僅供測試用途
class IndexAction extends Action {
public function index(){
header("Content-Type:text/html; charset=utf-8");
$this->display("reg");
}
function add(){
if(md5($_POST['verify'])!=$_SESSION['verify']){
$this->error("驗證碼錯誤");
}
//實例化自定義模型 M('User')實例化基礎模型
$user=D("User");
if($user->create()){
//執行插入操作,執行成功後,返回新插入的數據庫的ID
if($user->add()){
$this->success("注冊成功");
}else{
$this->error("注冊失敗");
}
}else{
//把錯誤信息提示給用戶看
$this->error($user->getError());
}
}
//生成圖片驗證碼
function verify(){
/**
* 在thinkPHP中如何實現驗證碼
*
* ThinkPHP已經為我們提供了圖像處理的類庫ThinkPHP\Extend\...
*
* 如何導入類庫?
* 導入類庫用"import(文件路徑)來導入,但是注意文件的路徑中的\要替換成 . 號"
* 1)導入系統的類庫 import(從library開始算起) import('ORG.Util.Image')注意大小寫
* 2)導入項目類庫 import("@.ORG.Image") 我們需要在我恩的項目的Lib目錄中存放
*/
//導入圖形處理類庫
import("ORG.Util.Image");
//import("@.ORG.Image");
//生成圖形驗證碼
/*
length:驗證碼的長度,默認為4位數
mode:驗證字符串的類型,默認為數字,其他支持類型有0 字母 1 數字 2 大寫字母 3 小寫字母 4中文 5混合(去掉了容易混淆的字符oOLl和數字01)
type:驗證碼的圖片類型,默認為png
width:驗證碼的寬度,默認會自動根據驗證碼長度自動計算
height:驗證碼的高度,默認為22
verifyName:驗證碼的SESSION記錄名稱,默認為verify
*/
//實現英文驗證碼
image::buildImageVerify(4,1,'gif',60,22,'verify');
//實現中文驗證碼
//image::GBVerify();
}
}
模型UserModel.class.php
<?php
class UserModel extends Model{
//自動驗證
protected $_validate=array(
//每個字段的詳細驗證內容
array("username","require","用戶名不能為空"),
array("username","checkLength","用戶名長度不符合要求",0,'callback'),
array("password","require","密碼不能為空"),
array("password","checkLength","密碼長度的要求是5~15位之間",0,'callback'),
array("password","repassword","兩次密碼輸入不一致",0,'confirm'),
array("qq","require","qq必須填寫"),
//array("cdate","require","時間不能為空",callback),
);
//自動填充
protected $_auto=array(
array("password","md5",3,'function'),
array("cdate","shijian",3,'callback'),
array("dizhi","getIp",3,'callback'),
);
//自定義驗證方法,來驗證用戶名的長度是否合法
//$date形參 可以寫成任意如 $AA $bb
function checkLength($data){
//$data裡存放的就是要驗證的用戶輸入的字符串
if(strlen($data)<5||strlen($data)>15){
return false;
}else{
return true;
}
}
//返回訪問者的IP地址
function getIp(){
return $_SERVER['REMOTE_ADDR'];
}
function shijian(){
return date("Y-m-d H:i:s");
}
}
模板reg.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注冊</title>
</head>
<body>
<form action="__URL__/add" method="post" >
<table width="407" height="424" align="center">
<th height="95"><H2>請認真填寫以下注冊信息</H2></th>
<tr>
<td><table height="273" align="center">
<tr>
<td width="74" align="right">用戶名:</td>
<td width="304" align="left"><input type="text" name="username"></td>
</tr>
<tr>
<td height="70" align="right">密碼:</td>
<td align="left"><input type="password" name="password"></td>
</tr>
<tr>
<td align="right">確認密碼:</td>
<td align="left"><input type="password" name="repassword"></td>
</tr>
<tr>
<td align="right">QQ:</td>
<td align="left"><input type="text" name="qq"></td>
</tr>
<tr>
<td align="right">驗證碼:</td>
<td align="left"><input type="text" name="verify" >
<img id="verify" alt="驗證碼" onClick="show()" src="__URL__/verify"><a href="javascript:show()">看不清楚</a></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交"></td>
</tr>
</table></td>
</tr>
</table>
</form>
</body>
</html>
<script>
function show(){
document.getElementById("verify").src="__URL__/verify/random"+Math.random();
}
</script>
如果還有不明白的地方,可以給我留言,我會詳細解答您留下的問題,謝謝關注
目錄結構如下
TP
--------ThinkPHP 文件夾
--------protal.php 這個文件叫protal.php
當運行protal.php時,會出現ThinkPHP的歡迎頁面,證明已經配置成功,同時目錄結果會發生變化
此時的目錄為
TP
--------ThinkPHP 文件夾
--------protal.php 入口文件(上邊那個文件)
--------protal 文件夾
生成的項目目錄結構和系統目錄類似,包括:
Common
項目公共文件目錄,一般放置項目的公共函數
Conf
項目配置目錄,項目所有的配置文件都放在這裡
Lang
項目語言包目錄(可選 如果不需要多語言支持 可刪除)
Lib
項目類庫目錄,通常包括Action和Model子目錄
Tpl
項目模板目錄,支持模板主題
Runtime
項目運行時目錄,包括Cache(模板緩存)、Temp(數據緩存)、Data(數據目錄)和Logs(日志文件)子目錄,如果存在分組的話,則首先是分組目錄。
mysql> SELECT FROM_UNIXTIME(875996580);
-> '1997-10-04 22:23:00'
mysql> SELECT UNIXTIME_TIMESTAMP('1997-10-04 22:23:00');
-> '875996580'