search
HomeBackend DevelopmentPHP TutorialAn explanation of how to save session data to memcache

An explanation of how to save session data to memcache

Mar 14, 2018 pm 12:46 PM
memcachesessionmethod

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;;
?>

Get_session.php

<?php
session_start();
$name=$_SESSION[&#39;name&#39;];
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[&#39;age&#39;]=&#39;whjwhj&#39;;
$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(&#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的

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;
?>

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

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!

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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools