Home > Article > Backend Development > This file is the entry file index.php, _PHP tutorial
This file is the entry file 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); ?>
There is a configuration file config.inc.php at the same level as the entry file
<?php return array( //'配置项'=>'配置值' 'DB_TYPE'=>'mysql', 'DB_HOST'=>'localhost', //数据库名 'DB_NAME'=>'think', //数据库用户 'DB_USER'=>'root', //数据库密码 'DB_PWD'=>'', //数据库端口 'DB_PORT'=>'3306', //表前缀 'DB_PREFIX'=>'t_', ) ?>
Controller 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(); } }
Model 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"); } }
Template 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>
If you still don’t understand anything, you can leave me a message and I will answer your questions in detail. Thank you for your attention
The directory structure is as follows
TP
--------ThinkPHP folder
--------protal.php This file is called protal.php
When protal.php is run, the ThinkPHP welcome page will appear, proving that the configuration has been successful, and the directory results will change at the same time
The directory at this time is
TP
--------ThinkPHP folder
--------protal.php entry file (the file above)
--------protal folder
The generated project directory structure is similar to the system directory, including:
Common
|
Project public file directory, generally places the project’s public functions
|
||||||||||||
Conf
|
Project configuration directory, all configuration files of the project are placed here
|
||||||||||||
Lang |
Project language pack directory (optional, can be deleted if multi-language support is not required)
|
||||||||||||
Lib
|
Project class library directory, usually including Action and Model subdirectories
|
||||||||||||
Tpl
|
Project template directory, supports template themes | ||||||||||||
Runtime | The project runtime directory includes Cache (template cache), Temp (data cache), Data (data directory) and Logs (log file) subdirectories. If there is a group, the group directory will be the first. |