您的位置:首页 > 编程语言 > PHP开发

ThinkPHP3.0主入口配置,注册、登录案例

2013-12-22 18:16 706 查看
转载于leyu.5ixiuxian.com

贴一个我的项目目录,对刚接触ThinkPHP3.0的同学很有帮助的



此文件时入口文件index.php

<span style="font-size:14px;"><?php

//定义一下ThinkPHP框架存放的路径
define('THINK_PATH','./ThinkPHP/');

//定义当前的项目的名称,此处的项目可理解为模块home理解为前台部分
define('APP_NAME','protal');

//定义项目的路径
define('APP_PATH','./protal/');

define('APP_DEBUG', true);

require THINK_PATH.'ThinkPHP.php';</span>

conf/config.php 

<span style="color:#ff6666;"><span style="font-size:14px;"><?php
//包含定义配置数据库连接的配置文件
$dbConf=include './config.inc.php';

//定义项目本身常规配置
$Conf=array(
//'配置项'=>'配置值'

'URL_MODEL'=>2,//2表示是URL重写模式

);
return array_merge($dbConf,$Conf);

?> </span></span>

与入口文件同级的有一个配置文件config.inc.php

<span style="font-size:14px;"><?php
return array(
//'配置项'=>'配置值'
'DB_TYPE'=>'mysql',

'DB_HOST'=>'localhost',
//数据库名
'DB_NAME'=>'think',
//数据库用户
'DB_USER'=>'root',
//数据库密码
'DB_PWD'=>'',
//数据库端口
'DB_PORT'=>'3306',
//表前缀
'DB_PREFIX'=>'t_',

)
?>  </span>

控制器IndexAction.class.php

<span style="font-size:14px;"><?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();
}

}  </span>

模型UserModel.class.php

<span style="font-size:14px;"><?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");
}

}  </span>

模板reg.html

<span style="font-size:14px;"><!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>  </span>


目录结构如下

 

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(日志文件)子目录,如果存在分组的话,则首先是分组目录。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  thinkphp javascript