ThinkPHP3.0 integration UCenter1.6 (2)

Source: Internet
Author: User

Because there is a project to be delivered in the past few days, it has been very busy and has no time to complete thinkphp3.0 integration with ucenter1.6. Today, we finally have time to integrate tp with uc, so that everyone can wait, sorry!

I previously published a thinkphp3.0 integration ucenter1.6 article, which was recognized by everyone. Some netizens reported that the integration was still not possible. I summarized it and found that I did not make it too clear last time, the final reason is that uc is not configured. all the paths in php have been modified. You can find uc. config. inc. php, and change it to config_ucenter.php in our own configuration file, and modify the corresponding path! In this way, the communication is successful!

If it is unsuccessful, you can leave a message for me or consult me directly. When I am not busy, I will reply to you!

Now, let's get down to the point. Now we start the formal integration! My idea is to encapsulate the APIS provided by ucenter to make our programs look clean and tidy! First, create a folder in the lib directory and name it Service. The functions of this folder will be explained later. Then create our own class in the Service to facilitate communication with UC and create a php file named UcService. class. php,

Class Name UcService. We implement UCenter operations in the class. To operate the UCenter API, first we need to import the corresponding file. I believe you have come up with it. The first is the UCenter configuration file: config_ucenter.php, the second is the client that my program communicates with UCenter. php file. Import these two files. We add a constant to the index. php entry file to define our physical and directory.

// Project physical and Path

Define ('doc _ ROOT_PATH ', rtrim (dirname (_ FILE _),'/\ '). DIRECTORY_SEPARATOR );

Then we define a constructor in UcService. class. php to import these two files,

Public function _ construct ()
{
Include_once (DOC_ROOT_PATH. 'conf/config_ucenter.php ');
Include_once (DOC_ROOT_PATH. 'uc _ client/client. php ');
}

OK! I believe that all the children's shoes have completed the above steps. Let's write the corresponding code to synchronize with the UCenter. First, let's complete the registration code.

// The user registers this method with UcService. class. php.

Public function register ($ username, $ password, $ email)
{
$ Uid = uc_user_register ($ username, $ password, $ email );
If ($ uid <= 0 ){
If ($ uid =-1 ){
Return 'invalid user name ';
} Elseif ($ uid =-2 ){
Return 'contains words that are not allowed to be registered ';
} Elseif ($ uid =-3 ){
Return 'user name already exists ';
} Elseif ($ uid =-4 ){
Return 'invalid Email format ';
} Elseif ($ uid =-5 ){
Return 'email registration not allowed ';
} Elseif ($ uid =-6 ){
Return 'this Email has been registered ';
} Else {
Return 'undefined ';
}
} Else {
Return intval ($ uid );
}
}

This function is UCenter registration verification. As long as we implement this class in the program, we can use this function to perform communication verification registration with UCenter.

So how do we use it?

First, my registration file is the register file. Open this file. If you do not have this file, create it. Under my directory, the Home with a group is the name directory of the group, if no shoes are configured, create them directly in the Action directory. This class must inherit the Action! Then we create a teach method function in the class to display a registration page.

Create a function to process the data submitted by the teach method function and add it to the database! Here is the main line!

Public teach (){

$ This-> display ();

}

Public function regDo (){

If ($ this-> isPost ()){
If (! $ _ REQUEST ['username'] |! $ _ REQUEST ['email '] |! $ _ REQUEST ['Password']) {
Exit ('data invalid .');
}
$ Nick = $ _ REQUEST ['username'];
$ Email = $ _ REQUEST ['email '];
$ Pw = trim ($ _ REQUEST ['Password']);

// This class needs to be configured in Conf/config. php. I will talk about it later!
$ UcService = new UcService;
$ Uid = $ ucService-> register ($ nick, $ pw, $ email );
// Successfully registered to UC
If (! Is_string ($ uid )){
$ Send_email = false;
$ Is_verifyed = 1;
// Register locally
$ UModel = D ('auser ');
$ Data ['user _ id'] = $ uid;
$ Data ['Nick '] = $ nick;
$ Data ['email '] = $ email;
$ Data ['Password'] = md5 ($ pw );
If ($ uModel-> add ($ data )){
$ This-> success ("registration successful! "," _ APP __/");
} Else {
$ This-> error ("registration failed! ");
}
} Else {
Exit ($ uid );
}

}
}

After local registration, students can directly write your program logic code. Before performing this step, add a sentence in Conf/config. php,

'App _ AUTOLOAD_PATH '=>' @. service ',

This statement tells ThinkPHP that the files in the Service directory under the Action will be automatically imported when the program is executed. If it is not imported, an error will be reported in our regDo! The system will prompt that the UcService class cannot be found. can we test whether the registration is successful?

We open the http://www.ttedu.com.cn/Register/teach to fill in the user name, password, mailbox, submit! OK! The program prompts us that the registration is successful! So let's go to the UCenter to see if user registration is successful? I just filled in the User name: test password: 123456 mailbox: ttedus@126.com IN THE ucenter user management we see has appeared we just registered the user, it means that we have registered successfully! The following is the login code! I am too tired to pack it in the Forum! Or leave your email address to me!

ThinkPHP3.0 UCenter integration tutorial (1)

Related Article

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.