How to restore session content through session_id in php

little bottle
Release: 2023-04-06 08:30:01
forward
4398 people have browsed it

This article mainly talks about restoring session content through session_id, which has certain reference value. Interested friends can learn about it.

1. Obtain session_id

// 开启session
session_start();
// 取得
$_SESSION['test'] = '111222333';
$session_id = session_id();
echo $session_id;exit;     // fu1dmdnrk0o2pi612b8jh9kts1
Copy after login

2. Obtain session through session_id

// 根据session_id查询session信息
session_id('fu1dmdnrk0o2pi612b8jh9kts1');
session_start();
print_r($_SESSION);exit;     // Array ( [test] => 111222333 )
Copy after login

One issue that needs attention here is: When obtaining session_id, you need to first session_start(); When obtaining the session through session_id, session_start() is required. Because session_start() will check whether there is currently an active session, if not, create a session and access it through the $_SESSION array. If the session already exists, the session_start() function will load the registered session variables so that they can be used.

Related tutorials: PHP video tutorial

php Chinese website learning topic: php session (Including pictures, texts, videos, cases)

The above is the detailed content of How to restore session content through session_id in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!