This article mainly introduces the thinkphp user registration login Message Complete example, help to learn more about the thinkphp operation process, the need for friends can refer to the following
In this paper, we describe the functions of thinkphp implementation including user registration, login and message, and we need to note that the D method is used when instantiating a user class in the presence of a user model .
UserActiion.class.php page:
<?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 (' Registration successful! ');} 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 to note, if it is the other field must be followed by a single quotation mark $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 (' Landing success! ');} else{$this->assign ("Jumpurl", "__app__/user/login "); $this->error (' Password error, please re-enter! ');}} else{$this->assign ("Jumpurl", "__app__/user/login"); $this->error (' username does not exist! ');}} Public Function Loginout () {Cookie::d elete (' username '); Cookies::d elete (' Lastlogintime '), $this->assign ("Jumpurl", "__app__/index/index"), $this->success (' You have successfully exited, Welcome to your next login! ');}}
IndexAction.class.php page:
<?php//This class is automatically generated by the system for testing purposes class Indexaction extends Action{public function Insert () {$content = new Contentmodel (); $re Sult = $content->create (), if (! $result) {$this->assign ("Jumpurl", "__url__/index"), $this->error ($content- >geterror ());//If the creation fails, indicating that the validation did not pass, the output error message}else{//validation passed, other operations $content->userid=cookie::get (' UserID '); $content- >add (); $this->assign ("Jumpurl", "__url__/index"); $this->success (' Add success! ');}} Data query Operation Public Function index () {$content = new Contentmodel (); $list = $content->findall (); The user's Cookie$username = cookie::get (' username '); $lastlogintime = Cookie::get (' lastlogintime '); $this->assign (' List ', $list); $this->assign (' title ', ' My Home '), $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 succeeded! ');} 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 of Create (), automatically will help you to data value/* $content->create (); $ Content->save (); Save modified data based on conditions echo "Update data successfully!" *///uses POST to update $id = $_post[' id '];if ($id! = ') {$data [' id '] = $id; $data [' title '] = $_post[' title ']; $data [' Content '] = $_post[' content '];if ($content->save ($data))//Save modified data by condition {$this->assign ("Jumpurl", "__url__/ Index "); $this->success (' Update data Success! ');} else{$this->assign ("Jumpurl", "__url__/index"); $this->success (' Update data failed! ');}} Else{echo "failed to save data! ";}}}? >
ContentModel.class.php page:
<?phpclass Contentmodel extends model{/** automatic verification * Array (validation fields, validation rules, error prompts, validation conditions, additional 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 field, fill content, fill condition, additional rule) */ Protected $_auto = Array (Array (' addtime ', ' time ', 1, ' function '),); >
UserModel.class.php page:
<?phpclass Usermodel extends model{protected $_validate = Array (Array (' username ', ' ', ' account name already exists! ', 0, ' unique ', 1),); }? >
It should be noted here that the use of automatic authentication when instantiated with $user = D ("user") instead of $user = m ("user"), with M this method will be an error, D function is used to instantiate the MODEL,M function user instantiation of 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" >