主要是在登入和退出的時候,設定cookies。來儲存登入和安全退出
1:在登入版面設定
//設定cookies的值
_setcookies($_rows['tg_username'], $_rows['tg_uniqid'],$_clean['time']);
_location(null,'index.php');
第二步:產生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; }}
第三步:登入狀態中設定
//登入的情況,就是防止登陸後通過在瀏覽器中直接連接再次登入function _login_state(){ if (!isset($_COOKIE['username'])) { _alert_back("登入狀態無法就行本操作!"); }}
//刪除cookiesfunction _unsetcookies(){ setcookie('username','',time()-1); setcookie('uniqid','',time()-1); _session_destroy();//刪除session _location(null,'index.php'); //做跳轉}
第四步在登入頁面和退出頁面加上
//登入狀態 _login_state();
以上就介紹了PHP練習項目筆記之COOKIES,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。