Example of using cookies in PHP (login)

Source: Internet
Author: User
Cookies are often used to identify users. A cookie is a small file that a server leaves on a user's computer. This computer sends a cookie whenever a page is requested by the same computer through a browser. With PHP, you can create and retrieve the value of a cookie

The main point is to set up cookies when logging in and exiting. To save logins and secure exits

1: Setup on login page

Set the value of the cookie _setcookies ($_rows[' tg_username '), $_rows[' tg_uniqid '],$_clean[' time ']); _location (null, ' index.php ') );

Step two: Generate Cookies,

/** * _setcookies * @param unknown $_username * @param unknown $_uniqid */function _setcookies ($_username,$_uniqid,$_time) {  Setcookie (' username ', $_username);  Setcookie (' uniqid ', $_uniqid);  Switch ($_time) {case    ' 0 '://Set Browser process      Setcookie (' username ', $_username);      Setcookie (' uniqid ', $_uniqid);      break;    Case ' 1 '://One-day process      Setcookie (' username ', $_username, $_time () + 86400);      Setcookie (' uniqid ', $_uniqid, $_time () + 86400);      break;    Case ' 2 '://week of Process      Setcookie (' username ', $_username, $_time () + 604800);      Setcookie (' uniqid ', $_uniqid, $_time () + 604800);      break;    Case ' 0 '://One-month process      Setcookie (' username ', $_username, $_time () + 2592000);      Setcookie (' uniqid ', $_uniqid, $_time () + 2592000);      break;  }}

Step three: Set up in the login state

The login situation is to prevent the login after logging in via the direct connection in the browser function _login_state () {  if (!isset ($_cookie[' username ')) {    _alert_back (" Login status cannot be done on this operation! ");  }} Delete Cookiesfunction _unsetcookies () {  Setcookie (' username ', ', Time ()-1);  Setcookie (' uniqid ', ', Time ()-1);  _session_destroy ()//delete session  _location (null, ' index.php ');//Do Jump}

The fourth step in the login page and exit page Plus

Login Status  _login_state ();

Two ways to display data: Configure it in the data profile, mine is mysql.func.php

/** * _fetch_array can only get one data group * @param $_sql */function _fetch_array ($_sql) {  return mysql_fetch_array (_query ($_sql), MYSQL_ASSOC);} /** * _fetch_array_list can return all data for the specified dataset * @param unknown $_result * @return Multitype: */function _fetch_array_list ($_resul T) {  return mysql_fetch_array ($_RESULT,MYSQL_ASSOC);}

Finally, assign a value to the page

//extract data from the database to get the result set//We must reread the result set every time, instead of executing the SQL statement from the new one. $_result = _query ("Select Tg_username,tg_sex,tg_face from Tg_user ORDER by Tg_reg_time DESC"); <?php while (!! $_rows = _fetch_array_list ($_result)) {?> <dl> <dd class= "user" ><?php echo $_rows[' Tg_username ']?> ; <?php echo $_rows[' tg_sex ']?></dd> <dt> 

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.