最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ThinkPHP之用戶注冊登錄留言完整實(shí)例

 更新時(shí)間:2014年07月22日 09:13:22   投稿:shichen2014  
這篇文章主要介紹了ThinkPHP之用戶注冊登錄留言完整實(shí)例,有助于詳細(xì)了解ThinkPHP的運(yùn)作流程,需要的朋友可以參考下

本文以實(shí)例形式講述ThinkPHP實(shí)現(xiàn)的包括用戶的注冊、登錄以及留言等功能,這里需要大家注意的是,在存在用戶模型的情況下實(shí)例化一個(gè)用戶類的時(shí)候使用D方法來實(shí)現(xiàn)。
 
UserActiion.class.php頁面:

<?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('注冊成功!');
}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 語句要注意一下,如果是其他字段的話后面一定要有單引號
$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ò),請重新輸入!');
}
}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頁面:

<?php
// 本類由系統(tǒng)自動生成,僅供測試用途
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','我的首頁');
$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','編輯頁面');
$this->display();
}
// 更新操作
public function update(){
$content = new ContentModel();
//直接使用create(),自動會幫你進(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頁面:

<?php
class ContentModel extends Model{
/*
* 自動驗(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)容必須填寫!'), 
);
/* 
* 自動填充
* array(填充字段,填充內(nèi)容,填充條件,附加規(guī)則)
*/
protected $_auto = array(
array('addtime','time',1,'function'),
);
}
?>

UserModel.class.php頁面:

<?php
class UserModel extends Model{
protected $_validate = array(
array('username','','帳號名稱已經(jīng)存在!',0,'unique',1), 
);  
}
?>

 
這里需要注意的是,使用自動驗(yàn)證的時(shí)候 實(shí)例化時(shí)要用 $user = D("user") 而不能用 $user = M("user"),用M這種方法會報(bào)錯(cuò),D函數(shù)用于實(shí)例化Model,M函數(shù)用戶實(shí)例化一個(gè)沒有模型的文件。
 
success.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">
<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秒后返回指定頁面!<br />
如果瀏覽器無法跳轉(zhuǎn),<a href="{$jumpUrl}" rel="external nofollow" >請點(diǎn)擊此處</a>。</p></td>
</tr>
</table>
</div>
</body>
</html>

相關(guān)文章

最新評論

沧州市| 富锦市| 新巴尔虎右旗| 瑞丽市| 轮台县| 博湖县| 花莲县| 台东县| 仁怀市| 青神县| 邵东县| 竹山县| 新密市| 射阳县| 上杭县| 大连市| 太白县| 宜阳县| 正蓝旗| 南通市| 蒲城县| 衡东县| SHOW| 丹寨县| 峨眉山市| 翁源县| 包头市| 德昌县| 沽源县| 兴业县| 武清区| 郯城县| 雷波县| 门头沟区| 西林县| 成安县| 洛阳市| 明星| 和平区| 盱眙县| 洛宁县|