1.验证码的生产与使用
Public function verify() {//PublicAction.class.php
import('ORG.Util.Image');
Image::buildImageVerify();
}
在需要显示验证码的网页上嵌入以下代码
<img src="__APP__/Public/verify/" width="50" height="24" />
<script type="text/javascript">
function show(obj) {//点击验证码图片后刷新验证码
obj.src = "__APP__/Public/verify/" + Math.random();
}
</script>
验证用户输入的验证码是否正确
if ($_SESSION['verify'] != md5($_POST['code'])) {
$this->error('验证码不正确');
}
2.用户登录验证部分代码
public function login() {
header("Content-Type:text/html;charset=utf-8");
$username = $_POST['username']; // 获取post变量
$password = $_POST['password']; // 获取post变量
//$code = $_POST['code']; // 获取post变量
if ($_SESSION['verify'] != md5($_POST['code'])) {
$this->error('验证码不正确');
}
$form = M('admin_user');//M函数
$admin_user = $form->where("username = '" . $username . "' and password = '" . md5($password) . "'")->find();//条件查询
if ($admin_user) {
$_SESSION['admin_user'] = $admin_user;
redirect('../index', 2, "<div align=\"center\" style=\"padding:100px\" ><font color=\"red\">登录成功!页面正在跳转中...</font></div>");
} else {
$this->error('用户名或密码不正确');
}
}
3.直接查询全部
public function chapter_list() {
$chapter = M('chapter'); // 实例化Data数据模型
$this->data = $chapter->select();
$this->display();
}
页面显示
<volist name="data" id="vo">
<tr>
<td height="20" bgcolor="#FFFFFF"><div align="center">
<input type="checkbox" name="checkbox10" id="checkbox10" />
</div></td>
<td height="20" bgcolor="#FFFFFF" class="STYLE19"><div align="center">{$vo.chapter_id}</div></td>
<td height="20" bgcolor="#FFFFFF" class="STYLE19"><div align="center">{$vo.chapter_title}</div></td>
<td height="20" bgcolor="#FFFFFF" class="STYLE19"><div align="center">{$vo.update_time}</div></td>
<td height="20" bgcolor="#FFFFFF"><div align="center"><span class="STYLE21"><a href="#">删除</a> | <a href="__APP__/Article/article_list?chapter_id={$vo.chapter_id}">查看</a></span></div></td>
</tr>
</volist>
4.url传值查询
public function article_list(){
$chapter_id=$_GET["chapter_id"];//url传值方式为get
$form = M('article');
$condition['chapter'] = $chapter_id;//条件查询
$this->data = $form->where($condition)->select();
$this->display();
}
5.删除数据
public function user_delete(){
header("Content-Type:text/html;charset=utf-8");
$username = $_GET['username'];//url传值
$User = M("user"); // 实例化User对象
$User->where('username=\''.$username.'\'')->delete(); // 删除username
redirect('../User/user_list', 2, "<div align=\"center\" style=\"padding:100px\" ><font color=\"red\">操作成功!页面正在跳转中...</font></div>");
}
6.更新数据
public function user_update() {
//header("Content-Type:text/html;charset=utf-8");
$Form = D('user');//表单提交过来的数据,action指向该方法
if ($Form->create()) {
$result = $Form->save();
if ($result) {
$this->success('操作成功!');
} else {
$this->error('没有更新数据!');
}
} else {
$this->error($Form->getError());
}
//redirect('../User/user_list', 2, "<div align=\"center\" style=\"padding:100px\" ><font color=\"red\">操作成功!页面正在跳转中...</font></div>");
}
7.数据分页
public function user_list() {
$Data = M('user'); // 实例化Data数据对象
import('ORG.Util.Page'); // 导入分页类
$count = $Data->where($map)->count(); // 查询满足要求的总记录数
$Page = new Page($count); // 实例化分页类 传入总记录数
// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
$nowPage = isset($_GET['p']) ? $_GET['p'] : 1;
$list = $Data->where($map)->order('username')->page($nowPage . ',' . $Page->listRows)->select();
$show = $Page->show(); // 分页显示输出
$this->assign('page', $show); // 赋值分页输出
$this->assign('list', $list); // 赋值数据集
$this->display(); // 输出模板
}
<volist name="list" id="vo">
<tr>
<td height="20" bgcolor="#FFFFFF"><div align="center">
<input type="checkbox" name="checkbox10" id="checkbox10" value="{$vo.username}"/>
</div></td>
<td height="20" bgcolor="#FFFFFF" class="STYLE19"><div align="center">{$vo.username}</div></td>
<td height="20" bgcolor="#FFFFFF" class="STYLE19"><div align="center">{$vo.truename}</div></td>
<td height="20" bgcolor="#FFFFFF" class="STYLE19"><div align="center">{$vo.sex}</div></td>
<td height="20" bgcolor="#FFFFFF" class="STYLE19"><div align="center">{$vo.age}</div></td>
<td height="20" bgcolor="#FFFFFF" class="STYLE19"><div align="center">{$vo.email}</div></td>
<td height="20" bgcolor="#FFFFFF">
<div align="center">
<a href="__APP__/User/user_delete?username={$vo.username}" confirm('确定删除?');"><span class="STYLE21">删除</span></a> |
<a href="__APP__/User/user_edit?username={$vo.username}"><span class="STYLE21">编辑</span></a> |
<a href="__APP__/User/user_view?username={$vo.username}"><span class="STYLE21">查看</span></a>
</div></td>
</tr>
</volist>
<strong> {$page}</strong>
<!--页码以及上一页下一页神马的在这里显示-->
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1340606