memcached提供了一个自定义的session处理器可以被用于存储用户session数据到memcached服务端。一个完全独立的memcached实例将会在内部使用,因此如果需要您可以设置一个不同的服务器池。session的key被存储在前缀memc.sess.key.之下,因此, 如果你对session和通常的缓存使用了同样的服务器池,请注意这一点。译注:另外一个session和通常缓存分离的原因是当通常的缓存占满了memcached服务端后,可能会导致你的session被从缓存中踢除,导致用户莫名的掉线。
session.save_handler string
设置为memcached开启memcached的session处理器。
session.save_path string
定义一个逗号分隔的hostname:port样式的session缓存服务器池,例如: "sess1:11211, sess2:11211".
方法I: 在 php.ini 中全局设置
session.save_handler = memcache session.save_path = "tcp://127.0.0.1:11211"
方法II: 某个目录下的 .htaccess
php_value session.save_handler "memcache" php_value session.save_path "tcp://127.0.0.1:11211"
方法III: 再或者在某个一个应用中
ini_set("session.save_handler", "memcache"); ini_set("session.save_path", "tcp://...:");
使用多个 memcached server 时用逗号","隔开,并且和 Memcache::addServer() 文档中说明的一样,可以带额外的参数"persistent"、"weight"、"timeout"、"retry_interval" 等等,类似这样的:"tcp://host1:port1?persistent=1&weight=2,tcp://host2:port2" 。
如果安装的PECL是memcached(依赖libmemcached库的那个扩展),则配置应为
ini_set("session.save_handler", "memcached"); // 是memcached不是memcache ini_set("session.save_path", "127.0.0.1:11211"); // 不要tcp:[/b]
代码例子(不依赖libmemcached库的那个)
<?php session_start(); if (!isset($_SESSION['TEST'])) { $_SESSION['TEST'] = time(); } $_SESSION['TEST'] = time(); print $_SESSION['TEST']; print "<br><br>"; print $_SESSION['TEST']; print "<br><br>"; print session_id(); ?>
用 sessionid 去 memcached 里查询一下:
<?php $memcache = memcache_connect('localhost', ); var_dump($memcache->get('ccedecbceebe')); $memcache->set('aaaa', 'hello everyone'); var_dump($memcache->get('aaaa')); ?>
会看到
string(37) "TEST|i:1177556731;TEST3|i:1177556881;"
这样的输出,证明 session 正常工作。
下面通过两种用法实例给大家介绍下php中使用memcached来存储session
一、
ini_set("session.save_handler", "memcache"); ini_set("session.save_path","tcp://127.0.0.1:11211");
多个memcached
ini_set("session.save_path","tcp://127.0.0.1:11211,tcp://127.0.0.1:11211");
二、
ini_set("session.save_handler", "memcached"); ini_set("session.save_path","...:");
多个memcached
ini_set("session.save_path","127.0.0.1:11211,127.0.0.1:11211");

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


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

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use
