Home > Article > Backend Development > Basic use of Session in php
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. Session
The 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([ 'cache_limiter' => 'private', 'read_and_close' => 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!