Home > Article > Backend Development > php - session control
Allows the server to make continuous requests based on the client.
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.
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
Create Cookie
<span>setCookie</span>("key","value",retainTime);<span>//</span><span>创建Cookie</span>
Call Cookie
<span>if</span> (<span>$_COOKIE</span>["key"] == "admin"<span>){ <span>//Cookie是php提供的超级数组 </span></span><span>echo</span> "获取数据成功"<span>; }</span>
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>
Cookie supports becoming a multi-dimensional array
<span>setCookie</span>("user[key]","values"); <span>//</span><span>相当于$_COOKIE["user"]["key"]</span>
Simple example: Cookie-based user login
Information is stored on the server rather than on individual PCs.
(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>
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>
(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();
The above introduces php - session control, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.