Complete example of ThinkPHP user registration Logon Message, thinkphp User Registration

Source: Internet
Author: User

Complete example of ThinkPHP user registration Logon Message, thinkphp User Registration

This article describes ThinkPHP's functions in the form of an instance, including user registration, logon, and message, 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", "_ APP _/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 (); pay attention to the where statement here, for other fields, you must enclose the 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 "); $ this-> success ('login successful! ');} Else {$ this-> assign ("jumpUrl", "_ APP _/user/login"); $ this-> error (' Password error, enter again! ') ;}} 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"); $ this-> success ('You have successfully exited, welcome to your next login! ');}}

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", "_ URL _/index"); $ this-> error ($ content-> getError ()); // If creation fails, the verification fails and an error message is returned.} else {// If the verification is successful, perform other operations $ content-> userid = Cookie :: get ('userid'); $ content-> add (); $ this-> assign ("jumpUrl", "_ URL _/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", "_ URL _/index"); $ this-> success ('deleted successfully! ');} Else {$ this-> assign ("jumpUrl", "_ URL _/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 according to the conditions {$ this-> assign ("jumpUrl", "_ URL _/index "); $ this-> success ('data updated successfully! ');} Else {$ this-> assign ("jumpUrl", "_ URL _/index"); $ this-> success (' failed to update data! ') ;}} 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:

<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

Php user registration login message problem !!

It depends on what you want. PHP is a language that can be integrated with scripts, you can write PHP code on your registration page. Of course, your file will be saved as a PHP file.
However, in your example, since the registration page has been completed in DW, we only need to select the ACTION attribute of FORM to place your php file location in it, as shown in <form action = '1. php 'method = 'post'>
In this way, the content of your message board will be submitted to the 1.php page, and the format of your page is HTML. the PHP page uses the POST method to obtain the content submitted by your form. Of course, you must first name The Name Of The submit variable. For example, we need to write a text box.
<Input type = 'text' name = 'text'>
The name attribute here is the name of the variable we get.
We use $ _ POST ['text'] to obtain the content you submitted.

ThinkPHP user registration problems

If it is null, only the data in the form should be submitted.
You need to identify the error: Does the table data key correspond to the database table key? Is the token Check of ThinkPHP enabled?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.