Complete example of ThinkPHP user registration login message

  • 2021-07-09 07:45:12
  • OfStack

In this paper, the functions of ThinkPHP, including user registration, login and message, are described in the form of examples. It is important for everyone to note that D method is used when instantiating a user class in the presence of a user model.

UserActiion. class. php page:


<?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(' Successful registration! ');
}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(); Here's where  Statement should pay attention to 1 Below, after if it is another field 1 Always have single quotation marks 
$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(' Login succeeded! ');
}else{
$this->assign("jumpUrl","__APP__/user/login");
$this->error(' Password error, please re-enter! ');
}
}else{
$this->assign("jumpUrl","__APP__/user/login");
$this->error(' User name does not exist! ');
}
}
public function loginout(){
Cookie::delete('username');
Cookie::delete('lastlogintime');
$this->assign("jumpUrl","__APP__/index/index");
$this->success(' You have successfully exited, welcome to log in next time! ');
}
}
 

IndexAction. class. php page:


<?php
//  This class is automatically generated by the system and is for testing purposes only 
class IndexAction extends Action{
public function insert() {   
$content = new ContentModel();
$result = $content->create();
if(!$result){
$this->assign("jumpUrl","__URL__/index");
$this->error($content->getError());// If the creation fails, it means that the verification failed and an error message is output 
}else{// Verify passed and perform other operations 
$content->userid=Cookie::get('userid');
$content->add();
$this->assign("jumpUrl","__URL__/index");
$this->success(' Add successfully! ');
}
} 
//  Data query operation   
public function index() {
$content = new ContentModel();
$list = $content->findAll();  
// User's cookie
$username = Cookie::get('username');
$lastlogintime = Cookie::get('lastlogintime');
$this->assign('list',$list);    
$this->assign('title',' My Home Page ');
$this->assign('username',$username);
$this->assign('lastlogintime',$lastlogintime);
$this->display();  
} 
//  Delete operation 
public function delete(){
$content = new ContentModel();
$id = $_GET['id'];
if($content->where("id=$id")->delete()){
$this->assign("jumpUrl","__URL__/index");
$this->success(' Delete successful! ');
}else{
$this->assign("jumpUrl","__URL__/index");
$this->error(' Delete failed! ');
}
} 
//  Edit operation 
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 " Data is empty! ";
}
}
$this->assign('title',' Edit page ');
$this->display();
}
//  Update operation 
public function update(){
$content = new ContentModel();
// Direct use create(), Automatically will help you to transfer the data value 
/*$content->create();
$content->save(); //  Save modified data according to conditions 
echo " Update data successfully! ";*/
//  Use post  Pass the value over and update it 
$id = $_POST['id'];
if($id != '')
{
$data['id'] = $id;
$data['title'] = $_POST['title'];
$data['content'] = $_POST['content'];
if($content->save($data))//  Save modified data according to conditions 
{
$this->assign("jumpUrl","__URL__/index");
$this->success(' Update data successfully! ');
}
else{
$this->assign("jumpUrl","__URL__/index");
$this->success(' Failed to update data! ');
}
}else
{
echo " Failed to save data! ";
}
}
}
?>
 

ContentModel. class. php page:


<?php
class ContentModel extends Model{
/*
*  Automatic validation 
* array( Validation Fields, Validation Rules, Error Prompts, Validation Criteria, Attached Rules, Validation Time )
*/ 
protected $_validate = array(
array('title','require',' Title must be filled in !'),
array('content','require',' Contents must be filled in !'), 
);
/* 
*  Autofill 
* array( Fill Fields, Fill Contents, Fill Criteria, Attach Rules )
*/
protected $_auto = array(
array('addtime','time',1,'function'),
);
}
?>

UserModel. class. php page:


<?php
class UserModel extends Model{
protected $_validate = array(
array('username','',' Account name already exists! ',0,'unique',1), 
);  
}
?>


It should be noted here that when using automatic verification, we should use $user = D ("user") instead of $user = M ("user"). If we use M, we will report an error. The D function is used to instantiate Model, and 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> Information prompt </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"> Information prompt </th>
</tr>
<tr>
<td><p>{$message}<br />
2 Seconds to return to the specified page! <br />
 If the browser cannot jump, <a href="{$jumpUrl}" rel="external nofollow" > Please click here </a> . </p></td>
</tr>
</table>
</div>
</body>
</html>

Related articles: