Uniform authentication between discuz and third-party applications

Source: Internet
Author: User

Recently, some projects have used the unified authentication of discuz and the project. They have made great efforts to study discuz.Source codeAnd passport authentication.

At the beginning, we used version 6.1. it was okay to follow the instruction manual for a long time to register. However, it was impossible to achieve the same login. After reading it for a long timeCode.

A little clear. But the cookies are not found in the session. You 'd better choose to stop using version 6.0. Both registration and unified authentication

It can be implemented in a simple way.

In some cases, update the source code. so I read the discuz code and page for a long time. ten again served discuz's code encapsulation. all operations are encapsulated into functions. ten minutes later. the search is dying. to understand what each function means, it takes a long time to find this function. however, I have to admire that the code running efficiency of this forum is indeed very good. currently, PHP is used for development. so this forum is worth my research and study. however, it may take a long time to understand the meaning of each function in this forum. to understand the code.

The code for unified authentication is attached. If you need it, you can learn it. 6.1 of the unified authentication did not study the value of regret.

<? PHP

//////////////////////////////////////// /////////////////////////////////////////
/**
* Passport encryption function
*
* @ Param string the original string waiting for encryption
* @ Param string private key (used for decryption and encryption)
*
* @ Return string the result of the original string encrypted with a private key
*/
Function passport_encrypt ($ txt, $ key ){

// Use the random number generator to generate 0 ~ 32000 value and MD5 ()
Srand (double) microtime () * 1000000 );
$ Encrypt_key = MD5 (RAND (0, 32000 ));

// variable initialization
$ CTR = 0;
$ TMP = '';

// For Loop, $ I is an integer starting from 0 to less than $ TXT String Length
for ($ I = 0; $ I // If $ CTR = $ encrypt_key length, then $ CTR is cleared
$ CTR = strlen ($ encrypt_key )? 0: $ CTR;
// Add two digits to the end of the TMP string. The first value is the $ CTR bit of $ encrypt_key,
// The second bit is the $ I bit of $ TXT and the $ CTR bit of $ encrypt_key. Then $ CTR = $ CTR + 1
$ TMP. = $ encrypt_key [$ CTR]. ($ TXT [$ I] ^ $ encrypt_key [$ CTR ++]);
}

// Return result. The result is the base64 encoded result returned by the passport_key () function.
Return base64_encode (passport_key ($ TMP, $ key ));
}

/**
* Passport decryption Function
*
* @ Param string the encrypted string
* @ Param string private key (used for decryption and encryption)
*
* @ Return string result after Private Key decryption
*/
Function passport_decrypt ($ txt, $ key ){

// $ TXT results in base64 decoding of the encrypted string, and then together with the private key,
// Return value after passport_key () function processing
$ TXT = passport_key (base64_decode ($ txt), $ key );

// Variable Initialization
$ TMP = '';

// For Loop, $ I is an integer starting from 0 to less than $ TXT String Length
For ($ I = 0; $ I <strlen ($ txt); $ I ++ ){
// $ TMP adds a bit at the end of the string, whose content is the $ I bit of $ TXT,
// It is the same as the $ I + 1 bits in $ TXT. Then $ I = $ I + 1
$ TMP. = $ TXT [$ I] ^ $ TXT [++ $ I];
}

// Return the value of $ TMP as the result
Return $ TMP;

}

/**
* Passport key processing function
*
* @ Param string the string to be encrypted or to be decrypted
* @ Param string private key (used for decryption and encryption)
*
* @ Return string the processed key
*/
Function passport_key ($ txt, $ encrypt_key ){

// Assign $ encrypt_key to the value of $ encrypt_key after MD5 ()
$ Encrypt_key = MD5 ($ encrypt_key );

// Variable Initialization
$ CTR = 0;
$ TMP = '';

// For Loop, $ I is an integer starting from 0 to less than $ TXT String Length
For ($ I = 0; $ I <strlen ($ txt); $ I ++ ){
// If $ CTR = $ encrypt_key length, $ CTR is cleared.
$ CTR = strlen ($ encrypt_key )? 0: $ CTR;
// $ TMP adds a bit at the end of the string, whose content is the $ I bit of $ TXT,
// It is the same or as the $ CTR + 1 of $ encrypt_key. Then $ CTR = $ CTR + 1
$ TMP. = $ TXT [$ I] ^ $ encrypt_key [$ CTR ++];
}

// Return the value of $ TMP as the result
Return $ TMP;

}

/**
* passport information (array) encoding function
* @ Param array the array to be encoded
* @ return string the encoded string of the array
*/< BR> function passport_encode ($ array) {
$ arrayenc = array ();
foreach ($ array as $ key => $ Val) {
// Add an element to the $ arrayenc array, the content is "$ key = $ Val value after urlencode ()"
$ arrayenc [] = $ key. '= '. urlencode ($ Val);
}< br> return implode ('&', $ arrayenc);
}

//
Function jswrite ($ MSG)
{
Echo "<script language = 'javascript '> ";
Echo $ MSG;
Echo "</SCRIPT> ";
}

Function jsgoto ($ URL)
{
$ MSG = "location. href = '$ url ';";
Jswrite ($ MSG );
Exit;
}
//
$ Passport_key = '';
$ Newusername = isset ($ _ Get ["username"])? $ _ Get ["username"]: "admin_test12 ";
$ Newpassword = isset ($ _ Get ["password"])? $ _ Get ["password"]: "111111 ";
$ Newemail = isset ($ _ Get ["email"])? $ _ Get ["email"]: "admin_test21@admin.com ";
$ Member = Array
('Cookietime' => time (),
'Time' => time (),
'Username' => $ newusername,
'Password' => MD5 (newpassword ),
'Email '=> newemail,
'Credit' => '',
'Regip' => '',
'Regdate' => '',
);
$ Action = 'login ';
$ Auth = passport_encrypt (passport_encode ($ member), $ passport_key );
$ Forward = 'HTTP: // 127.0.0.1/BBS/index. php ';
$ Verify = MD5 ($ action. $ Auth. $ forward. $ passport_key );
Jsgoto ("http: // 127.0.0.1/BBS/API/passport. php ".
"? Action = $ action ".
"& Auth =". rawurlencode ($ auth ).
"& Forward =". rawurlencode ($ forward ).
"& Verify = $ verify ");
?>

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.