PHP implementation method based on cookie to set the user to not operate the auto Exit function for 30 minutes

Source: Internet
Author: User
Tags ord set cookie
This article mainly introduces PHP to use cookies to set the user 30 minutes without the operation of automatic exit function, the need for friends can refer to the following

Login controller need to do the login successfully put the user ID and other information stored in the cookie:

$this->systemsetkey (' name ' = ' + $admin _info[' admin_name '), ' id ' = = $admin _info[' admin_id '], ' gid ' =>$ admin_info[' Admin_gid '), ' sp ' = $admin _info[' admin_is_super ']);//Do something after the landing

The Systemsetkey method in the parent class:

/**   * System background Member Login to write the member verification content to the corresponding cookie   *   * @param string $name user name   * @param int $id User ID   * @return bool Cloth The return result of the type   */  protected final function Systemsetkey ($user) {    Setnccookie (' Sys_key ', Encrypt (serialize ($ User), Md5_key), 3600, ", null);//Set cookie expiration time is 30 minutes. This way set the cookie framework with its own encryption rules, specifically whether you need to encrypt yourself to look at the settings.  }

The parent class controller constructs the method to determine whether the user has logged in:

protected function __construct () {language::read (' common,layout '); /** * Verify that the user is logged in * $admin _info Administrator profile Name ID */$this->admin_info = $this->systemlogin ();//obtain information of the administrator, after the    Subclass Controller Inheritance Constructor method if ($this->admin_info[' id ']! = 1) {//Verify Permissions $this->checkpermission (); }//transcoding prevents GBK from using AJAX calls simultaneous Kanji data garbled if (($_get[' branch ']!= ' | | $_get[' OP ']== ' Ajax ') && strtoupper (CHARSET) = = ' G    BK ') {$_get = LANGUAGE::GETGBK ($_get); }}/** * System background Login Verification * * @param * @return return result of array type */protected final function Systemlogin () {//Get COO Kie content, decryption, and system matching $user = Unserialize (Decrypt (Cookie (' Sys_key '), Md5_key), or//take cookie stored information, now use the framework to customize the cookie encryption method I F (!key_exists (' gid ', (array) $user) | |!isset ($user [' SP ']) | | (Empty ($user [' name ']) | | empty ($user [' ID ']))    {//If there is no indication that the user has not logged in or the user has not manipulated the cookie for a long time expires skip to the landing page to @header (' Location:index.php?mod=login&action=login '); exit; }else {$this->systemsetkey ($user);//If the user has logged in, each operation will rewrite the refreshCookies;} return $user; }

Cryptographic functions:

/** * Cryptographic Functions * * @param string $txt strings that need to be encrypted * @param string $key key * @return String returns the encryption result */function encrypt ($txt, $key =  ") {if (empty ($txt)) return $txt;  if (empty ($key)) $key = MD5 (Md5_key);  $chars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789-_.";  $ikey = "-X6G6ZWM2G9G_VR0BO.POQ3KRIXSZ6RM";  $NH 1 = rand (0,64);  $NH 2 = rand (0,64);  $NH 3 = rand (0,64);  $ch 1 = $chars {$nh 1};  $ch 2 = $chars {$nh 2};  $ch 3 = $chars {$NH 3};  $nhnum = $nh 1 + $nh 2 + $nh 3;  $knum = 0; $i = 0;  while (Isset ($key {$i})) $knum +=ord ($key {$i + +});  $mdKey = SUBSTR (MD5 (MD5 (MD5 ($key. $ch 1). $ch 2. $ikey). $ch 3), $nhnum%8, $knum%8 + 16); $txt = Base64_encode (Time ().  _ '. $txt);  $txt = str_replace (Array (' + ', '/', ' = '), array ('-', ' _ ', '. '), $txt);  $tmp = ";  $j =0; $k = 0;  $tlen = strlen ($txt);  $klen = strlen ($mdKey);    for ($i =0; $i < $tlen; $i + +) {$k = $k = = $klen? 0: $k;    $j = ($nhnum +strpos ($chars, $txt {$i}) +ord ($mdKey {$k + +}))%64;  $tmp. = $chars {$j};  } $tmplen = strlen ($tmp); $tmp = Substr_rePlace ($tmp, $ch 3, $NH 2 + + $tmplen, 0);  $tmp = Substr_replace ($tmp, $ch 2, $nh 1 + + $tmplen, 0);  $tmp = Substr_replace ($tmp, $ch 1, $knum% + + $tmplen, 0); return $tmp;}

Decryption function:

/** * Decryption Function * * @param string $txt strings to be decrypted * @param string $key key * @return The returned result of string string type */function decrypt ($txt, $k  EY = ", $ttl = 0) {if (empty ($txt)) return $txt;  if (empty ($key)) $key = MD5 (Md5_key);  $chars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789-_.";  $ikey = "-X6G6ZWM2G9G_VR0BO.POQ3KRIXSZ6RM";  $knum = 0; $i = 0;  $tlen = @strlen ($txt);  while (Isset ($key {$i})) $knum +=ord ($key {$i + +});  $ch 1 = @ $txt {$knum% $tlen};  $nh 1 = Strpos ($chars, $ch 1);  $txt = @substr_replace ($txt, ", $knum% $tlen--, 1);  $ch 2 = @ $txt {$nh 1 $tlen};  $nh 2 = @strpos ($chars, $ch 2);  $txt = @substr_replace ($txt, ", $nh 1 $tlen--, 1);  $ch 3 = @ $txt {$nh 2 $tlen};  $nh 3 = @strpos ($chars, $ch 3);  $txt = @substr_replace ($txt, ", $nh 2 $tlen--, 1);  $nhnum = $nh 1 + $nh 2 + $nh 3;  $mdKey = SUBSTR (MD5 (MD5 (MD5 ($key. $ch 1). $ch 2. $ikey). $ch 3), $nhnum% 8, $knum% 8 + 16);  $tmp = "; $j = 0;  $k = 0;  $tlen = @strlen ($txt);  $klen = @strlen ($mdKey); for ($i =0; $i < $tlen; $i + +) {$k = $k = = $kLen?    0: $k;    $j = Strpos ($chars, $txt {$i})-$nhnum-ord ($mdKey {$k + +});    while ($j <0) $j +=64;  $tmp. = $chars {$j};  } $tmp = Str_replace (Array ('-', ' _ ', '. '), Array (' + ', '/', ' = '), $tmp);  $tmp = Trim (Base64_decode ($tmp));      if (Preg_match ("/\d{10}_/s", substr ($tmp, 0,11))) {if ($ttl > 0 && (Time ()-substr ($tmp, 0,11) > $ttl)) {    $tmp = null;    }else{$tmp = substr ($tmp, 11); }} return $tmp;}

The above is a small part of the PHP to introduce the use of cookies to set the user 30 minutes did not operate the automatic exit function, we hope to help!

Related recommendations:

Knowledge and use of PHP cookies

Example code for login verification using PHP cookies

A note on the use of Php cookies, you know?

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.