An explanation of how to save session data to memcache

韦小宝
Release: 2023-03-21 11:32:02
Original
1374 people have browsed it

This article describes how the data in the session is stored in memcache. Many students may not know much about how the data in the session is stored in memcache. So today we will explain in detail how the data in the session is stored in memcache. Go to memcache!

Save the data of session to memcache

sessionThe data is saved in the file file by default

But we can modify the configuration of php to save it in other places

(1), open D:\lamp\php/php.ini. session.save_handler = files is open, comment it out

Session content saving path, add the red line That sentence

I modified it and tested it

Session.php

<?php
session_start();
$_SESSION[&#39;name&#39;]=&#39;whj&#39;;
?>
Copy after login

Get_session.php

<?php
session_start();
$name=$_SESSION[&#39;name&#39;];
echo $name;
?>
Copy after login

OutputwhjCorrect

(2), but how When obtaining the session variable, it is no longer the name attribute, but but through session_idTo save

session_id:When the browser accesses the server, the server assigns session_id to the browser and then passes session_idFind the corresponding value

Example:

session_start();
$_SESSION[&#39;age&#39;]=&#39;whjwhj&#39;;
$sess_id=session_id();
var_dump($sess_id);
//运行出来是string(26) "dmkppdo0qhbkq099fo608an383",在telnet中运行get dmkppdo0qhbkq099fo608an383的出age|s:6:"whjwhj";
Copy after login

If you don’t have permission to modify## What should I do with the configuration of #php?

ini_set() You can set some configurations of php in the php file

Security issue: Now anyone can access my memcache, others can also access my telnet 192.168.2.200,

Solution: Firewall

The php.ini configuration file was modified when using the above session. What is done now is There is no need to modify the configuration file and add directly to the beginning of the file:

ini_set(&#39;session.save_handler&#39;,&#39;memcache&#39;);
ini_set(&#39;session.save_path&#39;,&#39;tcp://127.0.0.1:11211&#39;);告诉它是用session保存到memcache的
Copy after login

Example:

Ini_session.php

<?php
ini_set(&#39;session.save_handler&#39;,&#39;memcache&#39;);
ini_set(&#39;session.save_path&#39;,&#39;tcp://127.0.0.1:11211&#39;);
session_start();
class Hot{
         public $name;
         public $color;
         public function __construct($name,$color){
                   $this->name=$name;
                   $this->color=$color;
                   }
         }
 $hot=new Hot(&#39;xiaobei&#39;,&#39;white&#39;);
 $_SESSION[&#39;hot&#39;]=$hot;
?>
Copy after login

Get_ini_session.php

<?php
ini_set(&#39;session.save_handler&#39;,&#39;memcache&#39;);
ini_set(&#39;session.save_path&#39;,&#39;tcp://127.0.0.1:11211&#39;);
session_start();
class Hot{
         public $name;
         public $color;
         public function __construct($name,$color){
                   $this->name=$name;
                   $this->color=$color;
                   }
         }
$hot=$_SESSION[&#39;hot&#39;];
var_dump($hot);
?>
Copy after login

7Memcache Life cycle:

Restarting memcached and restarting the operating system will cause all data to disappear. In addition, after the content capacity reaches the specified value, unused caches are automatically deleted based on the LRU (Least Recently Used) algorithm.

If expire is set to 0, it means it will never expire until the machine is restarted or the service is restarted

Related articles:
php session control session, cookie introduction

The above is the detailed content of An explanation of how to save session data to memcache. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!