Home  >  Article  >  Backend Development  >  Basic use of Session in php

Basic use of Session in php

autoload
autoloadOriginal
2021-03-16 13:48:314829browse

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

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,
   ]);
  ?>

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!

Statement:
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