PHP练习项目笔记之COOKIES

WBOY
发布: 2016-08-08 09:20:04
原创
982 人浏览过

主要是在登录和退出的时候,设置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>
登录后复制

<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>}
登录后复制

第四步在登录页面和退出页面加上

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

以上就介绍了PHP练习项目笔记之COOKIES,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!