ThinkPHP user registration login message complete example, thinkphp user registration_PHP tutorial

WBOY
Release: 2016-07-13 10:23:04
Original
686 people have browsed it

Complete example of ThinkPHP user registration, login and message, thinkphp user registration

This article uses examples to describe the functions implemented by ThinkPHP, including user registration, login, and leaving messages. What needs to be noted here is that Use the D method when instantiating a user class when there is a user model. Implement .

UserActivion.class.php page:

<&#63;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('您已经成功退出,欢迎您的下次登录!');
}
}
 
Copy after login

IndexAction.class.php page:

<&#63;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 "保存数据失败!";
}
}
}
&#63;>
 
Copy after login

ContentModel.class.php page:

<&#63;php
class ContentModel extends Model{
/*
* 自动验证
* array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
*/ 
protected $_validate = array(
array('title','require','标题必须填写!'),
array('content','require','内容必须填写!'), 
);
/* 
* 自动填充
* array(填充字段,填充内容,填充条件,附加规则)
*/
protected $_auto = array(
array('addtime','time',1,'function'),
);
}
&#63;>

Copy after login

UserModel.class.php page:

<&#63;php
class UserModel extends Model{
protected $_validate = array(
array('username','','帐号名称已经存在!',0,'unique',1), 
);  
}
&#63;>
Copy after login


What needs to be noted here is that when using automatic verification, you must use $user = D("user") instead of $user = M("user") when instantiating. Using the M method will report an error. The D function is used Instantiate Model, the M function user instantiates a file without a model.

success.html page:

<!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>
Copy after login

Problem with php user registration and login leaving messages!!

It depends on what you want. PHP is a language that can be integrated with scripts. You can write PHP code on the registration page you have made. Of course, in that case, your file will be saved as a PHP file
But in your example, since the registration page has been prepared in DW, we only need to select the ACTION attribute of the FORM form and put the location of your PHP file in it, such as


In this way, the content of your message board will be submitted to the 1.php page and your page format is HTML. We can use the POST method to obtain the content submitted by your form in the 1.PHP page. Of course, you must first Name the name of the submitted variable. For example, we write a text box

The name attribute here is the name of the variable we get
We use $ _POST['text'] to get the content you submitted is that simple

ThinkPHP user registration problem

If it is empty, you should only submit the data in the form.
The main thing you need to do now is to locate the error: Do the keys of the form data correspond to the keys of the database table? Also, has ThinkPHP's token check been turned on?

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/844131.htmlTechArticleThinkPHP user registration login message complete example, thinkphp user registration This article describes the implementation of ThinkPHP including user registration in the form of examples , login and message functions, this requires a lot of...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template