Home > Article > Backend Development > An explanation of how to save session data to memcache
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['name']='whj'; ?>
Get_session.php
<?php session_start(); $name=$_SESSION['name']; echo $name; ?>
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['age']='whjwhj'; $sess_id=session_id(); var_dump($sess_id); //运行出来是string(26) "dmkppdo0qhbkq099fo608an383",在telnet中运行get dmkppdo0qhbkq099fo608an383的出age|s:6:"whjwhj";
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('session.save_handler','memcache'); ini_set('session.save_path','tcp://127.0.0.1:11211');告诉它是用session保存到memcache的
Example:
Ini_session.php
<?php ini_set('session.save_handler','memcache'); ini_set('session.save_path','tcp://127.0.0.1:11211'); session_start(); class Hot{ public $name; public $color; public function __construct($name,$color){ $this->name=$name; $this->color=$color; } } $hot=new Hot('xiaobei','white'); $_SESSION['hot']=$hot; ?>
Get_ini_session.php
<?php ini_set('session.save_handler','memcache'); ini_set('session.save_path','tcp://127.0.0.1:11211'); session_start(); class Hot{ public $name; public $color; public function __construct($name,$color){ $this->name=$name; $this->color=$color; } } $hot=$_SESSION['hot']; var_dump($hot); ?>
7、Memcache 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!