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!

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

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.

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

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

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

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.

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools
