Home Backend Development PHP Tutorial PHP习题项目笔记之COOKIES

PHP习题项目笔记之COOKIES

Jun 13, 2016 pm 12:23 PM
fetch setcookie time

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

第三步:登录状态中设置

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

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

<span style="color: #008000;">//</span><span style="color: #008000;">登录状态</span>    _login_state();
Copy after login

 显示数据的二个方法:在数据配置文件中配置,我的是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>
Copy after login

最后在页面中赋值

<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>        
Copy after login
class="user">echo $_rows['tg_username']?>echo $_rows['tg_sex']?>
$_rows['tg_face']?>" alt="" />

 

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 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks 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)

What should I do if 'TypeError: Failed to fetch' occurs when using axios in a Vue application? What should I do if 'TypeError: Failed to fetch' occurs when using axios in a Vue application? Jun 24, 2023 pm 11:03 PM

Recently, during the development of Vue applications, I encountered a common problem: "TypeError: Failedtofetch" error message. This problem occurs when using axios to make HTTP requests and the backend server does not respond to the request correctly. This error message usually indicates that the request cannot reach the server, possibly due to network reasons or the server not responding. What should we do after this error message appears? Here are some workarounds: Check your network connection due to

Monotonic clock processing of time package Monotonic clock processing of time package Aug 04, 2023 pm 05:45 PM

Today we are mainly going to take a look at the time application method of golang time package. The general rule between the two is that "wall time" is used to tell time, and "monotonic clock" is used to measure time; there are other clock processing methods.

What to do if setcookie php fails What to do if setcookie php fails Oct 26, 2022 am 09:07 AM

Solution to the failure of setcookie php: 1. Open the corresponding PHP file; 2. Check whether the domain name parameters are standardized; 3. Implement it through global variables, code such as "foreach ($_COOKIE["cookie"] as $name => $value) {$name = htmlspecialchars($name);$value = htmlspecialchars($value);echo...".

How to use Java8 Time API How to use Java8 Time API Apr 28, 2023 pm 12:25 PM

1. Overview As part of this article, let us start with some problems with the existing Date and CalendarAPI and explore how the new Java8Date and TimeAPI solve these problems. We will also take a look at the core classes in the Java8 time class library, such as LocalDate, LocalTime, LocalDateTime, ZonedDateTime, Period, Duration and their APIs. 2. The problem of thread safety of the old time API (before Java 8)-Date and Calendar classes are not thread-safe, making it difficult for developers to debug concurrency problems of these APIs and need to write additional code to deal with them.

Data request and processing experience in Vue project development Data request and processing experience in Vue project development Nov 02, 2023 am 10:35 AM

In Vue project development, data request and processing are very important links. A good data request and processing strategy can greatly improve application performance and user experience. This article will share some experiences in data request and processing in Vue project development. Proper use of the Axios library Axios is a commonly used Promise-based HTTP library for sending XHR requests. In a Vue project, you can use Axios to send data requests. When using Axios, you need to pay attention to the following points: Properly configure A

How to use the setcookie function to set cookies in PHP How to use the setcookie function to set cookies in PHP Jun 26, 2023 pm 12:00 PM

In web development, cookies are a very common technology that allow web applications to store and access data on the client side. In PHP programming, setting cookies is usually implemented using the setcookie function. The syntax of the setcookie function is as follows: boolsetcookie(string$name[,string$value[,int$expire[,string$path[,

How to obtain and convert time in Python time module How to obtain and convert time in Python time module May 13, 2023 pm 12:19 PM

Pythontime module time acquisition and conversion Python's Time library can perform time-related processing, such as accessing the current date and time, outputting time in different formats, and waiting for a specified time. 1. Get the time 1.1. Timestamp importtimetimestamp=time.time()#1682737552.5009851 Greenwich Mean Time (GMT) The total number of seconds from 00:00:00 on January 1, 1970 to the present 1.2. Structured time importtimestruct_time= time.localtime()#time.struct_time(tm_year=2

How to use the fetch method in JavaScript How to use the fetch method in JavaScript Feb 19, 2024 pm 03:30 PM

Usage of fetch method in JavaScript In modern web development, data interaction with the server is a very common requirement. To meet this need, JavaScript provides the fetch method, which is a powerful and easy-to-use API that can help us send and receive HTTP requests. The basic usage of the fetch method is as follows: fetch(url,options).then(response=>res

See all articles