之前自己看了php基础就去审计,发现很多方法都不知道,审计呢也是审计不出洞,知识还没到位,便去看了TP框架开发,有助于审计的学习。
项目地址
https://www.php.cn/course/332.html
开发工具
JetBrains PhpStorm+navicat+phpstudy+tp3.2.3
流程简介
0X00 tp3.2.3介绍
目录
index.php 入口文件
README.md README文件
Application 应用目录
Public 资源文件目录
ThinkPHP 框架目录
Application
├─Common 应用公共模块
│ ├─Common 应用公共函数目录
│ └─Conf 应用公共配置文件目录
├─Home 默认生成的Home模块
│ ├─Conf 模块配置文件目录
│ ├─Common 模块函数公共目录
│ ├─Controller 模块控制器目录
│ ├─Model 模块模型目录
│ └─View 模块视图文件目录
├─Runtime 运行时目录
│ ├─Cache 模版缓存目录
│ ├─Data 数据目录
│ ├─Logs 日志目录
│ └─Temp 缓存目录
0X01
以后台文章为例
展示数据
现在application目录下增加一个后台Admin目录,将home目录copy并改名为Admin
分别在Controller,Model,View(需要新建,经典的mvc模型)建立文章的控制器,模块与视图
先在控制写出加载模板
$this->display();
访问后台看是否有界面出面,并在VIEW中将相应的代码更改,正确加载css,以便出现正常的界面
当有正确的界面时,这时去做控制器,完成后台功能的实现。首先配置正确的数据库连接。application->common->conf->config.php
简单的配置数据库的地址账号密码以及表前缀。
然后回到cate的控制器
application->admin->controller
先实现数据的加载,lst方法
$cate=D('cate'); //实例化数据
$cateres=$cate->order('sort desc')-> select();//通过查询使用sort排序
$this->assign('cateres',$cateres);//将查询的数据分配
$this->display();//展示模板
回到View下,将数据加载在页面中
application->admin->View->cate->lst.html
通过查询的数据,每条分配占一行,完成数据的填充
添加数据
先在控制器写出增加的方法
public function add(){
$cate=D('cate');//实例化
if(IS_POST){//判断是否在通过post传参
$date['catename']=I('catename');
if($cate->create($date)){//自动封装date数据
if ($cate->add()){
$this->success('添加成功',U('lst'));//返回lst
}
else{
$this->error('添加失败');
}
}
else{
$this->error($cate->getError());//指向model提示为空
}
return ;
}
$this->display();//数据展示
}
主要注意lst.html中name传参名是否对。
删除数据
public function del(){
$cate=D('cate');
if ($cate->delete(I('id'))){//通过ID删除数据
$this->success("删除成功",U('lst'));
}
else{
$this->error("删除失败");
}
}
修改数据
public function edit(){
$cate=D('cate');
$cater=$cate->find(I('id'));
$this->assign('cater',$cater);
if(IS_POST){
$date['id']=I('id');
$date['catename']=I('catename');
if($cate->create($date)){
if ($cate->save()){
$this->success('修改成功',U('lst'));
}
else{
$this->error('修改失败');
}
}
else{
$this->error($cate->getError());//指向model提示为空
}
return ;
}
$this->display();
}
后台内容的展示就是对数据库的增删改查,没有太难的地方。主要是前台页面中对值的分配与name传参name的正确性与否。
最后是model的编写
application->admin->model
主要完成值的判断
protected $_validate = array(
array('catename','require','添加不能为空!',1,'unique',3),
);
登录的实现
其他不再赘述,主要看登录的实现代码
登录的控制器代码
public function index(){
$admin=D("admin");
if (IS_POST) {
if ($admin->create($_POST)) {
if ($admin->login()) {
$this->success("登录成功", U("Index/index"));
} else {
$this->error("密码错误");
}
}
}
else{
$this->error($admin->getError());
}
return ;
$this->display("login");
}
主要是通过model的login方法判断是否登录成功。
看model的login方法
public function login(){
$password=$this->password;
$info=$this->where(array('username'=>$this->username))->find();//通过username找到该条数据,并以数组形式赋值给info。
if ($info){
if ($info["password"]==$password){//判断info数组中的password值与输入的password是否相等
session('id',$info[id]);
session('username',$info[username]);
return true;
}else{
return false;
}
}
else{
return false;
}
}
}
最后的感悟,项目的逻辑清晰,代码也较为简单,web就是对数据的增删改查,框架已经把数据库的增删改查已经简化,直接使用,不再需要自己select * from admin这样去查询赋值。还有就是多看开发手册
参考
https://www.php.cn/course/332.html
http://document.thinkphp.cn/manual_3_2.html#auto_build
若有不对,请及时指出。