Home > Backend Development > PHP Tutorial > php - session control

php - session control

WBOY
Release: 2016-07-29 09:01:42
Original
1018 people have browsed it

1. What is session control

Allows the server to make continuous requests based on the client.

2. Why do you need session control?

Because when you open a website and want to access other pages of the website, if there is no session control, you will need to enter your account and password again when you jump to other pages.

3. The principle and function of Cookie

Save the client’s simple information in the personal PC, and other programs obtain the PC’s Cookie to obtain the user’s information. This way there is no need for the user to enter the account and password themselves

Note: setCookie() must be used before php outputs the first sentence, otherwise it will be invalid

4. How to use Cooike (general situation)

Create Cookie

<span>setCookie</span>("key","value",retainTime);<span>//</span><span>创建Cookie</span>
Copy after login

Call Cookie

<span>if</span> (<span>$_COOKIE</span>["key"] == "admin"<span>){ <span>//Cookie是php提供的超级数组
  </span></span><span>echo</span> "获取数据成功"<span>;       
}</span>
Copy after login

Delete Cookie

<span>//</span><span>第一种方法</span><span>setCookie</span>("key");<span>//</span><span>只需要输入键名即可
//第二种方法</span><span>setCookie</span>("key","",<span>time</span>()-1000);<span>//</span><span>让保留的时间小于当前时间</span>
Copy after login

Cookie supports becoming a multi-dimensional array

<span>setCookie</span>("user[key]","values"); <span>//</span><span>相当于$_COOKIE["user"]["key"]</span>
Copy after login


Simple example: Cookie-based user login

5. The principle and function of Session

Information is stored on the server rather than on individual PCs.

6. How to use Session

(1). Configure php.ini options (do not expand, check the relevant documents yourself)

(2). Start session

<span>session_start</span>();<span>//</span><span>在使用session之前都必须先调用该方法</span>
Copy after login

Function: Configure the built-in environment related to Session Variables are preloaded into memory.

(3) Call

<span>$_SESSION</span>["key"] = "value";<span>//</span><span>$_SESSION也是超级数组,并以数组方式调用</span>
Copy after login

(4) Delete

<span>//</span><span>单个删除</span><span>unset</span>(<span>$_SESSION</span>["key"<span>]);
</span><span>//</span><span>全部删除</span><span>$_SESSION</span> = <span>array</span>(); <span>//</span><span>设置成空数组
//将这个用户在服务器端对应的Session文件删除</span>session_destory();
Copy after login

The above introduces php - session control, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template