Table of Contents
PHP练习项目笔记之COOKIES,php练习cookies
Home php教程 php手册 PHP练习项目笔记之COOKIES,php练习cookies

PHP练习项目笔记之COOKIES,php练习cookies

Jun 13, 2016 am 08:57 AM
cookies

PHP练习项目笔记之COOKIES,php练习cookies

主要是在登录和退出的时候,设置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>
Copy after login

第三步:登录状态中设置

<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>
Copy after login
<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>
}
Copy after login

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

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

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

<span>/*</span><span>*
 * _fetch_array 只能获取一条数据组
 * @param $_sql
 </span><span>*/</span>

<span>function</span> _fetch_array(<span>$_sql</span><span>) {
    </span><span>return</span> <span>mysql_fetch_array</span>(_query(<span>$_sql</span>),<span>MYSQL_ASSOC);
}
</span><span>/*</span><span>*
 * _fetch_array_list可以返回指定数据集的所有数据
 * @param unknown $_result
 * @return multitype:
 </span><span>*/</span>
<span>function</span> _fetch_array_list(<span>$_result</span><span>){
    </span><span>return</span> <span>mysql_fetch_array</span>(<span>$_result</span>,<span>MYSQL_ASSOC);
}</span>
Copy after login

最后在页面中赋值

<span>//</span><span>从数据库里提取数据获取结果集
//我们必须每次重新读取结果集,而不是从新去执行SQL语句。</span>
<span>$_result</span> = _query("SELECT tg_username,tg_sex,tg_face FROM tg_user ORDER BY tg_reg_time DESC"<span>);

</span><?php <span>while</span> (!!<span>$_rows</span> = _fetch_array_list(<span>$_result</span>)) {?>
    <<span>dl</span>>
        <dd <span>class</span>="user"><?php <span>echo</span> <span>$_rows</span>['tg_username']?><?php <span>echo</span> <span>$_rows</span>['tg_sex']?></dd>
        <dt><img  src="<?php echo <span alt="PHP练习项目笔记之COOKIES,php练习cookies" >$_rows</span>['tg_face']?>" alt="" /></dt>
Copy after login

 

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)