This article mainly introduces the complete example of ThinkPHP user registration and logon messages, which helps you to learn more about the operation process of ThinkPHP, if you need ThinkPHP, you can refer to the following example to describe ThinkPHP, including user registration, logon, and message functions, use the D method to instantiate a user class when a user model exists.
UserActiion. class. php page:
<? Phpclass UserAction extends Action {public function add () {$ user = D ("user"); $ user-> create (); $ result = $ user-> add (); if ($ result) {$ this-> assign ("jumpUrl", "/index. php/index "); $ this-> success ('registration successful! ');} Else {// echo $ user-> getError (); $ this-> assign ("jumpUrl", "/index. php/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 (); note the where statement here. if it is another field, there must be a single quotation mark $ userinfo = $ user-> wher E ("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", "/index. php/index "); $ this-> success ('login successful! ');} Else {$ this-> assign ("jumpUrl", "/index. php/user/login "); $ this-> error ('password error, please enter it again! ') ;}} Else {$ this-> assign ("jumpUrl", "/index. php/user/login"); $ this-> error ('user name does not exist! ') ;}} Public function loginout () {Cookie: delete ('Username'); Cookie: delete ('lastlogintime '); $ this-> assign ("jumpUrl", "/index. php/index "); $ this-> success ('you have successfully exited. welcome to log on next time! ');}}
IndexAction. class. php page:
<? Php // This class is automatically generated by the system and is for test purposes only. class IndexAction extends Action {public function insert () {$ content = new ContentModel (); $ result = $ content-> create (); if (! $ Result) {$ this-> assign ("jumpUrl", "/index. php/Article/index "); $ this-> error ($ content-> getError (); // If creation fails, the verification fails, output error message} else {// verification passed, perform other operations $ content-> userid = Cookie: get ('userid'); $ content-> add (); $ this-> assign ("jumpUrl", "/index. php/Article/index "); $ this-> success ('added successfully! ') ;}// 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 homepage'); $ this-> assign ('username ', $ username); $ this-> assign ('lastlogintime', $ lastlogintime); $ this-> display () ;}// delete public function delete () {$ content = new ContentModel (); $ id = $ _ GET ['id']; if ($ content-> where ("id = $ id")-> delete ()) {$ this-> assign ("jumpUrl", "/index. php/Article/index "); $ this-> success ('deleted successfully! ');} Else {$ this-> assign ("jumpUrl", "/index. php/Article/index"); $ this-> error (' deletion failed! ') ;}} // Edit the 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 blank! ";}}$ This-> assign ('title', 'Edit page'); $ this-> display ();} // update public function update () {$ content = new ContentModel (); // directly use create (), which automatically transfers data values to/* $ content-> create (); $ content-> save (); // The modified data is saved according to the conditions. echo "data is updated successfully! "; * // Use the post value to update $ id =$ _ POST ['id']; if ($ id! = '') {$ Data ['id'] = $ id; $ data ['title'] = $ _ POST ['title']; $ data ['content'] =_ _ POST ['content']; if ($ content-> save ($ data )) // Save the modified data {$ this-> assign ("jumpUrl", "/index. php/Article/index "); $ this-> success ('data updated successfully! ');} Else {$ this-> assign ("jumpUrl", "/index. php/Article/index"); $ this-> success ('data update failed! ') ;}} Else {echo "an error occurred while saving the data! ";}}?>
ContentModel. class. php page:
<? Phpclass ContentModel extends Model {/** automatic verification * array (verification field, verification rule, error prompt, verification condition, additional rule, verification time) */protected $ _ validate = array ('title', 'require ',' the title must be filled in! '), Array ('content', 'require', 'the content is required! '),);/** Auto fill * array (fill field, fill content, fill condition, add rules) */protected $ _ auto = array ('addtime ', 'Time', 1, 'Function'),) ;}?>
UserModel. class. php page:
<? Phpclass UserModel extends Model {protected $ _ validate = array ('username', '', 'account name already exists! ', 0, 'Unique', 1),) ;}?>
Note that $ user = D ("user") instead of $ user = M ("user") is used for instantiation during automatic verification "), if M is used, an error is reported. The D function is used to instantiate the Model. The M function user instantiates a file without a Model.
Success.html page:
Information prompt
| Information prompt |
{$ Message} 2 seconds later, return to the specified page! If the browser cannot jump, click here. |