Heim  >  Artikel  >  Backend-Entwicklung  >  PHP习题项目笔记之COOKIES

PHP习题项目笔记之COOKIES

WBOY
WBOYOriginal
2016-06-13 12:23:26817Durchsuche

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="" />

 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn