首頁 >後端開發 >php教程 >PHP練習項目筆記之COOKIES

PHP練習項目筆記之COOKIES

WBOY
WBOY原創
2016-08-08 09:20:04997瀏覽

主要是在登入和登出的時候,設定cookies。來儲存登入和安全登出

1:在登入頁面設定

//設定cookies的值
_setcookies($_rows['tg_username'], $_rows['tg_uniqid'],$_clean['time']);
_location(null,'index.php');

第二步:產生cookie,

<span>/*</span><span>*
 * _setcookies
 * @param unknown $_username
 * @param unknown $_uniqid
 </span><span>*/</span><span>function</span> _setcookies(<span>$_username</span>,<span>$_uniqid</span>,<span>$_time</span><span>){
    </span><span>setcookie</span>('username',<span>$_username</span><span>);
    </span><span>setcookie</span>('uniqid',<span>$_uniqid</span><span>);
    </span><span>switch</span> (<span>$_time</span><span>) {
        </span><span>case</span> '0' : <span>//</span><span> 设置浏览器进程</span><span>setcookie</span> ( 'username', <span>$_username</span><span> );
            </span><span>setcookie</span> ( 'uniqid', <span>$_uniqid</span><span> );
            </span><span>break</span><span>;
        </span><span>case</span> '1' : <span>//</span><span> 一天的进程</span><span>setcookie</span> ( 'username', <span>$_username</span>, <span>$_time</span> () + 86400<span> );
            </span><span>setcookie</span> ( 'uniqid', <span>$_uniqid</span>, <span>$_time</span> () + 86400<span> );
            </span><span>break</span><span>;
        </span><span>case</span> '2' : <span>//</span><span> 一周的进程</span><span>setcookie</span> ( 'username', <span>$_username</span>, <span>$_time</span> () + 604800<span> );
            </span><span>setcookie</span> ( 'uniqid', <span>$_uniqid</span>, <span>$_time</span> () + 604800<span> );
            </span><span>break</span><span>;
        </span><span>case</span> '0' : <span>//</span><span> 一个月的进程</span><span>setcookie</span> ( 'username', <span>$_username</span>, <span>$_time</span> () + 2592000<span> );
            </span><span>setcookie</span> ( 'uniqid', <span>$_uniqid</span>, <span>$_time</span> () + 2592000<span> );
            </span><span>break</span><span>;
    }
}</span>

第三步:登入狀態中設定

<span>//</span><span>登录的情况,就是防止登陆后通过在浏览器中直接连接再次登录</span><span>function</span><span> _login_state(){
    </span><span>if</span> (!<span>isset</span>(<span>$_COOKIE</span>['username'<span>])) {
        _alert_back(</span>"登录状态无法就行本操作!"<span>);
    }
}</span>

rrrereee頁加上

<span>//</span><span>删除cookies</span><span>function</span><span> _unsetcookies(){
    </span><span>setcookie</span>('username','',<span>time</span>()-1<span>);
    </span><span>setcookie</span>('uniqid','',<span>time</span>()-1<span>);
    _session_destroy();</span><span>//</span><span>删除session</span>    _location(<span>null</span>,'index.php'); <span>//</span><span>做跳转</span>}

以上就介紹了PHP練習計畫筆記之COOKIES,包含了面向的內容,希望對PHP教學有興趣的朋友有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn