|
本文以實例形式講述Thinkphp實現的包括用戶的注冊、登錄以及留言等功能,這里需要大家注意的是,在存在用戶模型的情況下實例化一個用戶類的時候使用D方法來實現。
UserActiion.class.php頁面:
<?phpclass 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('您已經成功退出,歡迎您的下次登錄!');}}
IndexAction.class.php頁面:
<?php// 本類由系統自動生成,僅供測試用途class IndexAction extends Action{public function insert() { $content = new ContentModel();$result = $content->create();if(!$result){$this->assign("jumpUrl","__URL__/index");$this->error($content->getError());//如果創建失敗,表示驗證沒有通過,輸出錯誤信息}else{//驗證通過,進行其他操作$content->userid=Cookie::get('userid');$content->add();$this->assign("jumpUrl","__URL__/index");$this->success('添加成功!');}} // 數據查詢操作 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 "數據為空!";}}$this->assign('title','編輯頁面');$this->display();}// 更新操作public function update(){$content = new ContentModel();//直接使用create(),自動會幫你進行數據的傳值/*$content->create();$content->save(); // 根據條件保存修改的數據echo "更新數據成功!";*/// 使用post 傳值過來,進行更新$id = $_POST['id'];if($id != ''){$data['id'] = $id;$data['title'] = $_POST['title'];$data['content'] = $_POST['content'];if($content->save($data))// 根據條件保存修改的數據{$this->assign("jumpUrl","__URL__/index");$this->success('更新數據成功!');}else{$this->assign("jumpUrl","__URL__/index");$this->success('更新數據失敗!');}}else{echo "保存數據失敗!";}}}?>
ContentModel.class.php頁面:
<?phpclass ContentModel extends Model{/** 自動驗證* array(驗證字段,驗證規則,錯誤提示,驗證條件,附加規則,驗證時間)*/ protected $_validate = array(array('title','require','標題必須填寫!'),array('content','require','內容必須填寫!'), );/* * 自動填充* array(填充字段,填充內容,填充條件,附加規則)*/protected $_auto = array(array('addtime','time',1,'function'),);}?>
UserModel.class.php頁面:
<?phpclass UserModel extends Model{protected $_validate = array(array('username','','帳號名稱已經存在!',0,'unique',1), ); }?>
這里需要注意的是,使用自動驗證的時候 實例化時要用 $user = D("user") 而不能用 $user = M("user"),用M這種方法會報錯,D函數用于實例化Model,M函數用戶實例化一個沒有模型的文件。
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 />如果瀏覽器無法跳轉,<a href="{$jumpUrl}" rel="external nofollow" >請點擊此處</a>。</p></td></tr></table></div></body></html>
php技術:ThinkPHP之用戶注冊登錄留言完整實例,轉載需保留來源!
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。