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

PHP practice project notes COOKIES

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

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,

/**
 * _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: Set

//登录的情况,就是防止登陆后通过在浏览器中直接连接再次登录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'); //做跳转}

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

//登录状态    _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