Compile a php extension instance

Source: Internet
Author: User
Recently, the company changed the yaf framework and suddenly became interested in using c to implement php expansion. If a function is stable, mature, and widely used, then we can try to use the extension implementation (not necessarily in each case). After writing the extension, we don't need to include it every time. The tool class is directly loaded into the memory as php starts. I used it this time.

Recently, the company changed the yaf framework and suddenly became interested in using c to implement php expansion. If a function is stable, mature, and widely used, then we can try to use the extension implementation (not necessarily in each case). After writing the extension, we don't need to include it every time. The tool class is directly loaded into the memory as php starts. I used it this time.

Recently, the company changed the yaf framework and suddenly became interested in using c to implement php expansion. If a function is stable, mature, and widely used, then we can try to use the extension implementation (not necessarily in each case). After writing the extension, we don't need to include it every time. The tool class is directly loaded into the memory as php starts.

This time, I wrote the user session Encryption Class As an extension of php. The user class is based on des encryption and is mainly implemented,

  1. IsLogin // determine whether to log on
  2. SetLogin // is an encrypted cookie.
  3. GetUid
  4. GetUsername

I name this project slime (Shi lime), the little moles in the brave fight, the sticky ......

OK. Let's take a look at the php implementation first, because we are php-> c, so let's take a look at the php implementation first, which is actually a very common user class.

1
 Key = $ key; 10 if ($ iv = "") {11 $ this-> iv = $ key; // $ key is used as iv12} else {13 $ this-> iv = $ iv by default; // mcrypt_create_iv (mcrypt_get_block_size (MCRYPT_DES, MCRYPT_MODE_CBC), MCRYPT_DEV_RANDOM ); 14} 15} 16 17 function encrypt ($ str) {18 // encrypted, returns an uppercase hexadecimal string 19 $ size = mcrypt_get_block_size (MCRYPT_DES, MCRYPT_MODE_CBC ); 20 $ str = $ this-> pkcs5Pad ($ str, $ size); 21 return strtoupper (bin2hex (mcrypt_cbc (MCR YPT_DES, $ this-> key, $ str, MCRYPT_ENCRYPT, $ this-> iv); 22} 23 24 function decrypt ($ str) {25 // decrypt 26 $ strBin = $ this-> hex2bin (strtolower ($ str); 27 $ str = mcrypt_cbc (MCRYPT_DES, $ this-> key, $ strBin, MCRYPT_DECRYPT, $ this-> iv); 28 $ str = $ this-> pkcs5Unpad ($ str); 29 return $ str; 30} 31 32 function hex2bin ($ hexData) {33 $ binData = ""; 34 for ($ I = 0; $ I <strlen ($ hexData); $ I ++ = 2) {35 $ binDat A. = chr (hexdec (substr ($ hexData, $ I, 2); 36} 37 return $ binData; 38} 39 40 function pkcs5Pad ($ text, $ blocksize) {41 $ pad = $ blocksize-(strlen ($ text) % $ blocksize); 42 return $ text. str_repeat (chr ($ pad), $ pad); 43} 44 45 function pkcs5Unpad ($ text) {46 $ pad = ord ($ text {strlen ($ text) -1}); 47 if ($ pad> strlen ($ text) 48 return false; 49 if (strspn ($ text, chr ($ pad), st Rlen ($ text)-$ pad )! = $ Pad) 50 return false; 51 return substr ($ text, 0,-1 * $ pad); 52} 53}

Let's look at Session_User.

1
 Login_url = "http: // {$ host}/login"; 16} 17 else {18 $ this-> login_url = $ login_url; 19} 20 21 if (! $ Domain) {22 $ domain = $ _ SERVER ["SERVER_NAME"]; 23 $ this-> domain = $ domain; 24} 25 26 if (empty ($ _ COOKIE [$ this-> auth_name]) {27 return; 28} 29 30 list ($ uid, $ username, $ ua, $ tm, $ chineseName) =@ $ this-> decodeAuth ($ _ COOKIE [$ this-> auth_name]); 31 32 33 // ua Test 34 if (empty ($ uid) | $ ua! = Md5 ($ _ SERVER ['HTTP _ USER_AGENT ']) {35 return; 36} 37 38 // TODO: validity Period test 39 40 $ this-> uid = $ uid; 41 $ this-> username = $ username; 42 $ this-> chineseName = $ chineseName; 43} 44 45 static public function instance ($ login_url = null, $ domain = null) {46 if (self: $ obj) 47 return self: $ obj; 48 else {49 self: $ obj = new Session_User ($ login_url, $ domain); 50} 51 return self: $ obj; 52} 53 54 55/** 56 * Whether the user logs in to 57 **/58 public function isLogin () {59 if (! Empty ($ this-> uid) 60 return true; 61 else 62 return false; 63} 64/** 65*66 * jump to the logon page 67 * @ param unknown_type $ forward 68 * @ param unknown_type $ exit 69 */70 public function requireLogin ($ forward ='', $ exit = true) {71 if (! $ This-> isLogin () {72 if ($ forward = null) 73 {74 header ("location :". $ this-> login_url); 75 76} 77 else 78 {79 if (empty ($ forward) 80 {81 $ forward = 'HTTP ://'. $ _ SERVER ['HTTP _ host']. $ _ SERVER ['request _ URI ']; 82} 83 $ forward = urlencode ($ forward); 84 header ("location :". $ this-> login_url. "? Forward = $ forward "); 85} 86 if ($ exit) 87 exit; 88} 89} 90/** 91*92 * Set logon status 93 * @ param unknown_type $ uid 94 * @ param unknown_type $ username 95 * @ param unknown_type $ ua 96 * @ param unknown_type $ outtime 97 */98 99 public function setLogin ($ uid, $ username, $ ua = null, $ outtime = null, $ chineseName = null) {100 if (empty ($ ua )) {101 $ ua = $ _ SERVER ['HTTP _ USER_AGENT ']; 102} 103 104 $ str = $ this-> encode Auth ($ uid, $ username, $ ua, $ chineseName); 105 setcookie ($ this-> auth_name, urlencode ($ str), $ outtime ,'/', $ this-> domain); 106} 107/** 108 * User exits 109 */110 public function setLogout () {111 setcookie ($ this-> auth_name ,'', -1, '/', $ this-> domain); 112} 113 114 public function _ get ($ key) {115 if ('uid' = $ key) {116 return $ this-> uid; 117} elseif ('username' = $ key) {118 return $ this-> username; 119} elseif ('chinesen Ame' = $ key) {120 return $ this-> chineseName; 121} 122 return; 123} 124 125 public function getUid () {126 return $ this-> uid; 127} 128 129 public function getUserName () {130 return $ this-> username; 131} 132 133 public function getChineseName () {134 return $ this-> chineseName; 135} 136 137/** 138 * generate encrypted login cookie139 */140 private function encodeAuth ($ uid, $ username, $ ua, $ chineseName = null) {141 $ tm = time (); 14 2 $ ua = md5 ($ ua); 143 $ info = "$ uid \ t $ username \ t $ ua \ t $ tm \ t $ chineseName "; 144 $ des = new Session_DES (); 145 $ str = $ des-> encrypt ($ info); 146 return $ str; 147} 148 149/** 150 * parse encryption cookie 151 */152 private function decodeAuth ($ str) {153 $ des = new Session_DES (); 154 $ info = explode ("\ t", @ $ des-> decrypt ($ str); 155 if (is_array ($ info) {156 return $ info; 157} else {158 return array (); 159} 160} 161 162 public func Tion auth ($ controller, $ action) 163 {164 if (! In_array ($ controller, $ conArr) {165 return false; 166} 167 if (! In_array ($ action, $ actArr) {168 return false; 169} 170 return true; 171} 172}

View Code

Combine the above two pieces of code to separate the actually obtained session classes (the same and different sessions are often separated ), we only need three variables, two of which are the iv offset used by des, the key encryption key, and the cookie name. Then we can write our extensions, where we step on a lot of pitfalls, some makefiles, config. w4 and other small syntaxes, lib dependencies, in short, still learned a lot

Address: https://github.com/lietdai/ldclass

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.