您好,登錄后才能下訂單哦!
這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)怎么在ThinkPHP項目中實現(xiàn)一個用戶注冊登錄留言功能,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
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('密碼出錯,請重新輸入!'); } }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)建失敗,表示驗證沒有通過,輸出錯誤信息 }else{//驗證通過,進行其他操作 $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(),自動會幫你進行數(shù)據(jù)的傳值 /*$content->create(); $content->save(); // 根據(jù)條件保存修改的數(shù)據(jù) echo "更新數(shù)據(jù)成功!";*/ // 使用post 傳值過來,進行更新 $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ù)失?。?quot;; } } } ?>
ContentModel.class.php頁面:
<?php class ContentModel extends Model{ /* * 自動驗證 * array(驗證字段,驗證規(guī)則,錯誤提示,驗證條件,附加規(guī)則,驗證時間) */ protected $_validate = array( array('title','require','標題必須填寫!'), 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), ); } ?>
這里需要注意的是,使用自動驗證的時候 實例化時要用 $user = D("user") 而不能用 $user = M("user"),用M這種方法會報錯,D函數(shù)用于實例化Model,M函數(shù)用戶實例化一個沒有模型的文件。
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" > <tr> <th align="center" >信息提示</th> </tr> <tr> <td><p>{$message}<br /> 2秒后返回指定頁面!<br /> 如果瀏覽器無法跳轉(zhuǎn),<a href="{$jumpUrl}" rel="external nofollow" >請點擊此處</a>。</p></td> </tr> </table> </div> </body> </html>
上述就是小編為大家分享的怎么在ThinkPHP項目中實現(xiàn)一個用戶注冊登錄留言功能了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。