Home > Backend Development > PHP Tutorial > This file is the entry file index.php, _PHP tutorial

This file is the entry file index.php, _PHP tutorial

WBOY
Release: 2016-07-12 08:51:09
Original
1059 people have browsed it

This file is the entry file index.php,

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';


Copy after login


conf/config.php

<?php
//包含定义配置数据库连接的配置文件
$dbConf=include './config.inc.php';

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

	'URL_MODEL'=>2,//2表示是URL重写模式
		
);
return array_merge($dbConf,$Conf);

?>
Copy after login


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_',
		
)	
?>
Copy after login


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();
	}

	
	

}
Copy after login


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");
		}
	
	
	
}
Copy after login


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>


Copy after login


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

项目公共文件目录,一般放置项目的公共函数

Conf

项目配置目录,项目所有的配置文件都放在这里

Lang

项目语言包目录(可选 如果不需要多语言支持 可删除)

Lib

项目类库目录,通常包括Action和Model子目录

Tpl

项目模板目录,支持模板主题

Runtime

项目运行时目录,包括Cache(模板缓存)、Temp(数据缓存)、Data(数据目录)和Logs(日志文件)子目录,如果存在分组的话,则首先是分组目录。

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

mysql>
SELECT FROM_UNIXTIME(875996580);
-> '1997-10-04 22:23:00' mysql> SELECT UNIXTIME_TIMESTAMP('1997-10-04 22:23:00'); -> '875996580' http://www.bkjia.com/PHPjc/1132202.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1132202.htmlTechArticleThis file is the entry file index.php, This file is the entry file index.php?php//Define ThinkPHP The path where the framework is stored define('THINK_PATH','./ThinkPHP/');//Define the name of the current project...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template