ThinkPHP之用戶注冊(cè)登錄留言完整實(shí)例
本文以實(shí)例形式講述ThinkPHP實(shí)現(xiàn)的包括用戶的注冊(cè)、登錄以及留言等功能,這里需要大家注意的是,在存在用戶模型的情況下實(shí)例化一個(gè)用戶類的時(shí)候使用D方法來實(shí)現(xiàn)。
UserActiion.class.php頁(yè)面:
<?php
class UserAction extends Action{
public function add(){
$user = D("user");
$user->create();
$result = $user->add();
if($result){
$this->assign("jumpUrl","__APP__/index/index");
$this->success('注冊(cè)成功!');
}else{
//echo $user->getError();
$this->assign("jumpUrl","__APP__/user/register");
$this->error($user->getError());
}
}
public function register(){
$this->display();
}
public function login(){
$this->display();
}
public function checklogin(){
$username = $_POST['username'];
$passwd = $_POST['passwd'];
$user = D("user");
//$User->where('id=8')->find();這里的where 語(yǔ)句要注意一下,如果是其他字段的話后面一定要有單引號(hào)
$userinfo = $user->where("username ='$username'")->find();
if(!empty($userinfo)){
if($userinfo['passwd'] == $passwd){
Cookie::set('userid',$userinfo['id'],time()+3600*24);
Cookie::set('username',$username,time()+3600*24);
Cookie::set('lastlogintime',time(),time()+3600*24);
$this->assign("jumpUrl","__APP__/index/index");
$this->success('登陸成功!');
}else{
$this->assign("jumpUrl","__APP__/user/login");
$this->error('密碼出錯(cuò),請(qǐng)重新輸入!');
}
}else{
$this->assign("jumpUrl","__APP__/user/login");
$this->error('用戶名不存在!');
}
}
public function loginout(){
Cookie::delete('username');
Cookie::delete('lastlogintime');
$this->assign("jumpUrl","__APP__/index/index");
$this->success('您已經(jīng)成功退出,歡迎您的下次登錄!');
}
}
IndexAction.class.php頁(yè)面:
<?php
// 本類由系統(tǒng)自動(dòng)生成,僅供測(cè)試用途
class IndexAction extends Action{
public function insert() {
$content = new ContentModel();
$result = $content->create();
if(!$result){
$this->assign("jumpUrl","__URL__/index");
$this->error($content->getError());//如果創(chuàng)建失敗,表示驗(yàn)證沒有通過,輸出錯(cuò)誤信息
}else{//驗(yàn)證通過,進(jìn)行其他操作
$content->userid=Cookie::get('userid');
$content->add();
$this->assign("jumpUrl","__URL__/index");
$this->success('添加成功!');
}
}
// 數(shù)據(jù)查詢操作
public function index() {
$content = new ContentModel();
$list = $content->findAll();
//用戶的cookie
$username = Cookie::get('username');
$lastlogintime = Cookie::get('lastlogintime');
$this->assign('list',$list);
$this->assign('title','我的首頁(yè)');
$this->assign('username',$username);
$this->assign('lastlogintime',$lastlogintime);
$this->display();
}
// 刪除操作
public function delete(){
$content = new ContentModel();
$id = $_GET['id'];
if($content->where("id=$id")->delete()){
$this->assign("jumpUrl","__URL__/index");
$this->success('刪除成功!');
}else{
$this->assign("jumpUrl","__URL__/index");
$this->error('刪除失?。?);
}
}
// 編輯操作
public function edit(){
$content = new ContentModel();
$id = $_GET['id'];
if($id != '')
{
//$data = $content->select($id);
$data = $content->where("id=$id")->select();
if(!empty($data)){
$this->assign('data',$data);
}else{
echo "數(shù)據(jù)為空!";
}
}
$this->assign('title','編輯頁(yè)面');
$this->display();
}
// 更新操作
public function update(){
$content = new ContentModel();
//直接使用create(),自動(dòng)會(huì)幫你進(jìn)行數(shù)據(jù)的傳值
/*$content->create();
$content->save(); // 根據(jù)條件保存修改的數(shù)據(jù)
echo "更新數(shù)據(jù)成功!";*/
// 使用post 傳值過來,進(jìn)行更新
$id = $_POST['id'];
if($id != '')
{
$data['id'] = $id;
$data['title'] = $_POST['title'];
$data['content'] = $_POST['content'];
if($content->save($data))// 根據(jù)條件保存修改的數(shù)據(jù)
{
$this->assign("jumpUrl","__URL__/index");
$this->success('更新數(shù)據(jù)成功!');
}
else{
$this->assign("jumpUrl","__URL__/index");
$this->success('更新數(shù)據(jù)失??!');
}
}else
{
echo "保存數(shù)據(jù)失敗!";
}
}
}
?>
ContentModel.class.php頁(yè)面:
<?php
class ContentModel extends Model{
/*
* 自動(dòng)驗(yàn)證
* array(驗(yàn)證字段,驗(yàn)證規(guī)則,錯(cuò)誤提示,驗(yàn)證條件,附加規(guī)則,驗(yàn)證時(shí)間)
*/
protected $_validate = array(
array('title','require','標(biāo)題必須填寫!'),
array('content','require','內(nèi)容必須填寫!'),
);
/*
* 自動(dòng)填充
* array(填充字段,填充內(nèi)容,填充條件,附加規(guī)則)
*/
protected $_auto = array(
array('addtime','time',1,'function'),
);
}
?>
UserModel.class.php頁(yè)面:
<?php
class UserModel extends Model{
protected $_validate = array(
array('username','','帳號(hào)名稱已經(jīng)存在!',0,'unique',1),
);
}
?>
這里需要注意的是,使用自動(dòng)驗(yàn)證的時(shí)候 實(shí)例化時(shí)要用 $user = D("user") 而不能用 $user = M("user"),用M這種方法會(huì)報(bào)錯(cuò),D函數(shù)用于實(shí)例化Model,M函數(shù)用戶實(shí)例化一個(gè)沒有模型的文件。
success.html頁(yè)面:
<!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">
<meta http-equiv="refresh" content="20; url='{$jumpUrl}'" />
<title>信息提示</title>
</head>
<body>
<div id="man_zone">
<table width="40%" border="1" align="center" cellpadding="3" cellspacing="0" class="table" style="margin-top:100px;">
<tr>
<th align="center" style="background:#cef">信息提示</th>
</tr>
<tr>
<td><p>{$message}<br />
2秒后返回指定頁(yè)面!<br />
如果瀏覽器無法跳轉(zhuǎn),<a href="{$jumpUrl}" rel="external nofollow" >請(qǐng)點(diǎn)擊此處</a>。</p></td>
</tr>
</table>
</div>
</body>
</html>
相關(guān)文章
PHP生成自定義長(zhǎng)度隨機(jī)字符串的函數(shù)分享
這篇文章主要介紹了PHP生成自定義長(zhǎng)度隨機(jī)字符串的函數(shù)分享,需要的朋友可以參考下2014-05-05
Laravel框架實(shí)現(xiàn)多數(shù)據(jù)庫(kù)連接操作詳解
這篇文章主要介紹了Laravel框架實(shí)現(xiàn)多數(shù)據(jù)庫(kù)連接操作,結(jié)合實(shí)例形式詳細(xì)分析了Laravel框架連接2個(gè)數(shù)據(jù)庫(kù)的具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-07-07
PHP模板引擎Smarty內(nèi)建函數(shù)section,sectionelse用法詳解
這篇文章主要介紹了PHP模板引擎Smarty內(nèi)建函數(shù)section,sectionelse用法,結(jié)合實(shí)例形式詳細(xì)分析了section,sectionelse進(jìn)行循環(huán)處理的技巧與使用方法,需要的朋友可以參考下2016-04-04

