thinkphp是不套php程序框架,代碼架構簡單,可操作性強,有很多站長朋友用這套系統搭建網站!這篇文章給大家講解ThinkPHP用戶注冊登錄留言完整實例,希望可以幫助到有所需要的朋友!
這里需要大家注意的是,在存在用戶模型的情況下實例化一個用戶類的時候使用D方法來實現。
UserActiion.class.php頁面:
01
<?php
02
class UserAction extends Action{
03
public function add(){
04
$user = D("user");
05
$user->create();
06
$result = $user->add();
07
if($result){
08
$this->assign("jumpUrl","__APP__/index/index");
09
$this->success('注冊成功!');
10
}else{
11
//echo $user->getError();
12
$this->assign("jumpUrl","__APP__/user/register");
13
$this->error($user->getError());
14
}
15
}
16
public function register(){
17
$this->display();
18
}
19
public function login(){
20
$this->display();
21
}
22
public function checklogin(){
23
$username = $_POST['username'];
24
$passwd = $_POST['passwd'];
25
$user = D("user");
26
//$User->where('id=8')->find();這里的where 語句要注意一下,如果是其他字段的話后面一定要有單引號
27
$userinfo = $user->where("username ='$username'")->find();
28
if(!empty($userinfo)){
29
if($userinfo['passwd'] == $passwd){
30
Cookie::set('userid',$userinfo['id'],time()+3600*24);
31
Cookie::set('username',$username,time()+3600*24);
32
Cookie::set('lastlogintime',time(),time()+3600*24);
33
$this->assign("jumpUrl","__APP__/index/index");
34
$this->success('登陸成功!');
35
}else{
36
$this->assign("jumpUrl","__APP__/user/login");
37
$this->error('密碼出錯,請重新輸入!');
38
}
39
}else{
40
$this->assign("jumpUrl","__APP__/user/login");
41
$this->error('用戶名不存在!');
42
}
43
}
44
public function loginout(){
45
Cookie::delete('username');
46
Cookie::delete('lastlogintime');
47
$this->assign("jumpUrl","__APP__/index/index");
48
$this->success('您已經成功退出,歡迎您的下次登錄!');
49
}
50
}
IndexAction.class.php頁面:
01
<?php
02
// 本類由系統自動生成,僅供測試用途
03
class IndexAction extends Action{
04
public function insert() {
05
$content = new ContentModel();
06
$result = $content->create();
07
if(!$result){
08
$this->assign("jumpUrl","__URL__/index");
09
$this->error($content->getError());//如果創建失敗,表示驗證沒有通過,輸出錯誤信息
10
}else{//驗證通過,進行其他操作
11
$content->userid=Cookie::get('userid');
12
$content->add();
13
$this->assign("jumpUrl","__URL__/index");
14
$this->success('添加成功!');
15
}
16
}
17
// 數據查詢操作
18
public function index() {
19
$content = new ContentModel();
20
$list = $content->findAll();
21
//用戶的cookie
22
$username = Cookie::get('username');
23
$lastlogintime = Cookie::get('lastlogintime');
24
$this->assign('list',$list);
25
$this->assign('title','我的首頁');
26
$this->assign('username',$username);
27
$this->assign('lastlogintime',$lastlogintime);
28
$this->display();
29
}
30
// 刪除操作
31
public function delete(){
32
$content = new ContentModel();
33
$id = $_GET['id'];
34
if($content->where("id=$id")->delete()){
35
$this->assign("jumpUrl","__URL__/index");
36
$this->success('刪除成功!');
37
}else{
38
$this->assign("jumpUrl","__URL__/index");
39
$this->error('刪除失??!');
40
}
41
}
42
// 編輯操作
43
public function edit(){
44
$content = new ContentModel();
45
$id = $_GET['id'];
46
if($id != '')
47
{
48
//$data = $content->select($id);
49
$data = $content->where("id=$id")->select();
50
if(!empty($data)){
51
$this->assign('data',$data);
52
}else{
53
echo "數據為空!";
54
}
55
}
56
$this->assign('title','編輯頁面');
57
$this->display();
58
}
59
// 更新操作
60
public function update(){
61
$content = new ContentModel();
62
//直接使用create(),自動會幫你進行數據的傳值
63
/*$content->create();
64
$content->save(); // 根據條件保存修改的數據
65
echo "更新數據成功!";*/
66
// 使用post 傳值過來,進行更新
67
$id = $_POST['id'];
68
if($id != '')
69
{
70
$data['id'] = $id;
71
$data['title'] = $_POST['title'];
72
$data['content'] = $_POST['content'];
73
if($content->save($data))// 根據條件保存修改的數據
74
{
75
$this->assign("jumpUrl","__URL__/index");
76
$this->success('更新數據成功!');
77
}
78
else{
79
$this->assign("jumpUrl","__URL__/index");
80
$this->success('更新數據失?。?);
81
}
82
}else
83
{
84
echo "保存數據失??!";
85
}
86
}
87
}
88
?>
ContentModel.class.php頁面:
01
<?php
02
class ContentModel extends Model{
03
/*
04
* 自動驗證
05
* array(驗證字段,驗證規則,錯誤提示,驗證條件,附加規則,驗證時間)
06
*/
07
protected $_validate = array(
08
array('title','require','標題必須填寫!'),
09
array('content','require','內容必須填寫!'),
10
);
11
/*
12
* 自動填充
13
* array(填充字段,填充內容,填充條件,附加規則)
14
*/
15
protected $_auto = array(
16
array('addtime','time',1,'function'),
17
);
18
}
19
?>
UserModel.class.php頁面:
1
<?php
2
class UserModel extends Model{
3
protected $_validate = array(
4
array('username','','帳號名稱已經存在!',0,'unique',1),
5
);
6
}
7
?>
這里需要注意的是,使用自動驗證的時候 實例化時要用 $user = D("user") 而不能用 $user = M("user"),用M這種方法會報錯,D函數用于實例化Model,M函數用戶實例化一個沒有模型的文件。
success.html頁面:
01
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
02
<html>
03
<head>
04
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
05
<meta http-equiv="refresh" content="20; url='{$jumpUrl}'" />
06
<title>信息提示</title>
07
</head>
08
<body>
09
<div id="man_zone">
10
<table width="40%" border="1" align="center" cellpadding="3" cellspacing="0"class="table" style="margin-top:100px;">
11
<tr>
12
<th align="center" style="background:#cef">信息提示</th>
13
</tr>
14
<tr>
15
<td><p>{$message}<br />
16
2秒后返回指定頁面!<br />
17
如果瀏覽器無法跳轉,<a href="{$jumpUrl}" rel="external nofollow" >請點擊此處</a>。</p></td>
18
</tr>
19
</table>
20
</div>
21
</body>
22
</html>
新聞熱點
疑難解答