首页 > 后端开发 > php教程 > PHP习题项目笔记之COOKIES

PHP习题项目笔记之COOKIES

WBOY
发布: 2016-06-13 12:23:26
原创
837 人浏览过

PHP练习项目笔记之COOKIES

主要是在登录和退出的时候,设置cookies。来保存登录和安全退出

1:在登录页面设置

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

第二步:生成cookie,

<span style="color: #008000;">/*</span><span style="color: #008000;">* * _setcookies * @param unknown $_username * @param unknown $_uniqid </span><span style="color: #008000;">*/</span><span style="color: #0000ff;">function</span> _setcookies(<span style="color: #800080;">$_username</span>,<span style="color: #800080;">$_uniqid</span>,<span style="color: #800080;">$_time</span><span style="color: #000000;">){    </span><span style="color: #008080;">setcookie</span>('username',<span style="color: #800080;">$_username</span><span style="color: #000000;">);    </span><span style="color: #008080;">setcookie</span>('uniqid',<span style="color: #800080;">$_uniqid</span><span style="color: #000000;">);    </span><span style="color: #0000ff;">switch</span> (<span style="color: #800080;">$_time</span><span style="color: #000000;">) {        </span><span style="color: #0000ff;">case</span> '0' : <span style="color: #008000;">//</span><span style="color: #008000;"> 设置浏览器进程</span>            <span style="color: #008080;">setcookie</span> ( 'username', <span style="color: #800080;">$_username</span><span style="color: #000000;"> );            </span><span style="color: #008080;">setcookie</span> ( 'uniqid', <span style="color: #800080;">$_uniqid</span><span style="color: #000000;"> );            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;        </span><span style="color: #0000ff;">case</span> '1' : <span style="color: #008000;">//</span><span style="color: #008000;"> 一天的进程</span>            <span style="color: #008080;">setcookie</span> ( 'username', <span style="color: #800080;">$_username</span>, <span style="color: #800080;">$_time</span> () + 86400<span style="color: #000000;"> );            </span><span style="color: #008080;">setcookie</span> ( 'uniqid', <span style="color: #800080;">$_uniqid</span>, <span style="color: #800080;">$_time</span> () + 86400<span style="color: #000000;"> );            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;        </span><span style="color: #0000ff;">case</span> '2' : <span style="color: #008000;">//</span><span style="color: #008000;"> 一周的进程</span>            <span style="color: #008080;">setcookie</span> ( 'username', <span style="color: #800080;">$_username</span>, <span style="color: #800080;">$_time</span> () + 604800<span style="color: #000000;"> );            </span><span style="color: #008080;">setcookie</span> ( 'uniqid', <span style="color: #800080;">$_uniqid</span>, <span style="color: #800080;">$_time</span> () + 604800<span style="color: #000000;"> );            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;        </span><span style="color: #0000ff;">case</span> '0' : <span style="color: #008000;">//</span><span style="color: #008000;"> 一个月的进程</span>            <span style="color: #008080;">setcookie</span> ( 'username', <span style="color: #800080;">$_username</span>, <span style="color: #800080;">$_time</span> () + 2592000<span style="color: #000000;"> );            </span><span style="color: #008080;">setcookie</span> ( 'uniqid', <span style="color: #800080;">$_uniqid</span>, <span style="color: #800080;">$_time</span> () + 2592000<span style="color: #000000;"> );            </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;    }}</span>
登录后复制

第三步:登录状态中设置

<span style="color: #008000;">//</span><span style="color: #008000;">登录的情况,就是防止登陆后通过在浏览器中直接连接再次登录</span><span style="color: #0000ff;">function</span><span style="color: #000000;"> _login_state(){    </span><span style="color: #0000ff;">if</span> (!<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_COOKIE</span>['username'<span style="color: #000000;">])) {        _alert_back(</span>"登录状态无法就行本操作!"<span style="color: #000000;">);    }}</span>
登录后复制
<span style="color: #008000;">//</span><span style="color: #008000;">删除cookies</span><span style="color: #0000ff;">function</span><span style="color: #000000;"> _unsetcookies(){    </span><span style="color: #008080;">setcookie</span>('username','',<span style="color: #008080;">time</span>()-1<span style="color: #000000;">);    </span><span style="color: #008080;">setcookie</span>('uniqid','',<span style="color: #008080;">time</span>()-1<span style="color: #000000;">);    _session_destroy();</span><span style="color: #008000;">//</span><span style="color: #008000;">删除session</span>    _location(<span style="color: #0000ff;">null</span>,'index.php'); <span style="color: #008000;">//</span><span style="color: #008000;">做跳转</span>}
登录后复制

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

<span style="color: #008000;">//</span><span style="color: #008000;">登录状态</span>    _login_state();
登录后复制

 显示数据的二个方法:在数据配置文件中配置,我的是mysql.func.php

<span style="color: #008000;">/*</span><span style="color: #008000;">* * _fetch_array 只能获取一条数据组 * @param $_sql </span><span style="color: #008000;">*/</span><span style="color: #0000ff;">function</span> _fetch_array(<span style="color: #800080;">$_sql</span><span style="color: #000000;">) {    </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">mysql_fetch_array</span>(_query(<span style="color: #800080;">$_sql</span>),<span style="color: #000000;">MYSQL_ASSOC);}</span><span style="color: #008000;">/*</span><span style="color: #008000;">* * _fetch_array_list可以返回指定数据集的所有数据 * @param unknown $_result * @return multitype: </span><span style="color: #008000;">*/</span><span style="color: #0000ff;">function</span> _fetch_array_list(<span style="color: #800080;">$_result</span><span style="color: #000000;">){    </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">mysql_fetch_array</span>(<span style="color: #800080;">$_result</span>,<span style="color: #000000;">MYSQL_ASSOC);}</span>
登录后复制

最后在页面中赋值

<span style="color: #008000;">//</span><span style="color: #008000;">从数据库里提取数据获取结果集//我们必须每次重新读取结果集,而不是从新去执行SQL语句。</span><span style="color: #800080;">$_result</span> = _query("SELECT tg_username,tg_sex,tg_face FROM tg_user ORDER BY tg_reg_time DESC"<span style="color: #000000;">);</span><?php <span style="color: #0000ff;">while (!!<span style="color: #800080;">$_rows</span> = _fetch_array_list(<span style="color: #800080;">$_result</span>)) {?>    dl>        
登录后复制
class="user">echo $_rows['tg_username']?>echo $_rows['tg_sex']?>
$_rows['tg_face']?>" alt="" />

 

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板