Cookies are often used to identify users. A cookie is a small file that a server leaves on a user's computer. Each time the same computer requests a page through the browser, the cookie will be sent to the computer. Through PHP, you can create and retrieve cookie values
Mainly set cookies when logging in and out. To save login and SecurityExit
1: Set
//设置cookies的值 _setcookies($_rows['tg_username'], $_rows['tg_uniqid'],$_clean['time']); _location(null,'index.php');
on the login page Step 2: Generate cookie,
/** * _setcookies * @param unknown $_username * @param unknown $_uniqid */ function _setcookies($_username,$_uniqid,$_time){ setcookie('username',$_username); setcookie('uniqid',$_uniqid); switch ($_time) { case '0' : // 设置浏览器进程 setcookie ( 'username', $_username ); setcookie ( 'uniqid', $_uniqid ); break; case '1' : // 一天的进程 setcookie ( 'username', $_username, $_time () + 86400 ); setcookie ( 'uniqid', $_uniqid, $_time () + 86400 ); break; case '2' : // 一周的进程 setcookie ( 'username', $_username, $_time () + 604800 ); setcookie ( 'uniqid', $_uniqid, $_time () + 604800 ); break; case '0' : // 一个月的进程 setcookie ( 'username', $_username, $_time () + 2592000 ); setcookie ( 'uniqid', $_uniqid, $_time () + 2592000 ); break; } }
Step 3: Log in Set
//登录的情况,就是防止登陆后通过在浏览器中直接连接再次登录 function _login_state(){ if (!isset($_COOKIE['username'])) { _alert_back("登录状态无法就行本操作!"); } } //删除cookies function _unsetcookies(){ setcookie('username','',time()-1); setcookie('uniqid','',time()-1); _session_destroy();//删除session _location(null,'index.php'); //做跳转 }
in the status. The fourth step is to add
//登录状态 _login_state();
to the login page and exit page. Two methods for displaying data: configure in the data configuration file , my It is mysql.func.php
/** * _fetch_array 只能获取一条数据组 * @param $_sql */ function _fetch_array($_sql) { return mysql_fetch_array(_query($_sql),MYSQL_ASSOC); } /** * _fetch_array_list可以返回指定数据集的所有数据 * @param unknown $_result * @return multitype: */ function _fetch_array_list($_result){ return mysql_fetch_array($_result,MYSQL_ASSOC); }
Finally assigned value in the page
//从数据库里提取数据获取结果集 //我们必须每次重新读取结果集,而不是从新去执行SQL语句。 $_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><img src="<?php echo $_rows['tg_face']?>" alt="" /></dt>
The above is the detailed content of Example of using COOKIES in php (login). For more information, please follow other related articles on the PHP Chinese website!