Home >Backend Development >PHP Tutorial >PHP practice project notes COOKIES

PHP practice project notes COOKIES

WBOY
WBOYOriginal
2016-08-08 09:20:04997browse

Mainly set cookies when logging in and out. To save login and safe exit

1: Set on the login page

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

Step 2: Generate 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>

Step 3: Set

<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>

<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>}

in login status Step 4 on login page and logout Page plus

<span>//</span><span>登录状态</span>    _login_state();

The above introduces the COOKIES of PHP practice project notes, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn