Basic use of Session in php

autoload
Release: 2023-04-09 20:08:02
Original
4844 people have browsed it

1.Session Introduction

session The session in PHP is driven by a unique session ID, which is a Encrypted random numbers, generated by PHP, are stored on the client for the lifetime of the session. SessionThe information is stored in the server side, but the session id is stored in the client cookie, of course PHP The session storage method is diversified, so even if cookie is disabled, it can still be tracked.

2.Session configuration and application

session_start();        //初始化session.需在文件头部
 
$_SESSION[name]=value;  //配置Seeeion
echo $_SESSION[name];   //使用session
isset($_SESSION[name]); // 判断
unset($_SESSION[name]); //删除
 
session_destroy();      //消耗所有session
Copy after login

3.PHP7 Session options

InPHP7 version (and later), the session_start() function can accept an array of options to override the session configuration directives set in php.ini . These options support session.lazy_write, which by default takes the value true, which causes PHP to overwrite any session files if the session data has changed. Another option added to the session_start() function is read_and_close, which indicates that the session data should be read and then the session will be closed immediately. For example, set session.cache_limiter to private and set the flag to close the session immediately after reading, using the snippet below.

<?php
   session_start([     
  &#39;cache_limiter&#39; => &#39;private&#39;,      
  &#39;read_and_close&#39; => true,
   ]);
  ?>
Copy after login

Recommended: php video tutorial

The above is the detailed content of Basic use of Session in php. For more information, please follow other related articles on the PHP Chinese website!

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