Php session into memcached

Source: Internet
Author: User
Php session into memcached windows7 32-bit environment required software


1. install Apache2.4 + PHP5.6
Configure httpd. conf in Apache2.4

Reference: http://jingyan.baidu.com/article/fcb5aff797ec41edaa4a71c4.html

ServerRoot "c:/Apache24"
Listen 8080.
DocumentRoot "c:/Apache24/htdocs"

# Php5 support
LoadModule php5_module c:/php-5.6.15-Win32-VC11-x86/php5apache2_4.dll
AddType application/x-httpd-php. php. html. htm
# Configure the path to php. ini
PHPIniDir "c:/php-5.6.15-Win32-VC11-x86"

Apsaradb for memcache stores data for more than 30 days. 0 indicates that the data will never expire. The maximum expire value is 30*3600*24 seconds.
Bool Memcache: add (string $ key, mixed $ var [, int $ flag [, int $ expire])

Time () + number of days * 3600*24




Memcached has no user name or password mechanism. enable firewall to block port 11211
Allow local access to port 11211 only
Iptables-a input-p tcp-s 127.0.0.1-dport 11211-j ACCEPT


II. session entry into memcached

// Traditional code
Session_start ();
$ _ SESSION ['name'] = 'tianlong Babu ';
// Retrieve
$ Name = $ _ SESSION ['name'];
Echo "name = $ name ";
?>

In traditional php. ini, configure sessions to be stored in files.

[Session]
Session. save_handler = files
Session. save_path = "c:/windows/temp"

Modify the php. ini file and load php_memcache.dll (the version must be the same as that of php and put it in the ext directory under the php installation directory)

Extension = C: \ php-5.6.15-Win32-VC11-x86 \ ext \ php. memcache. dll

Session. save_handler = memcache
Session. save_path = "tcp: // fig: 11211"

Memcache code for session entry

Session_start ();
$ _ SESSION ['name'] = 'Dragon, eight parts, 100 ';
$ _ SESSION ['city'] = 'Beijing ';

// If session data is input to mem, it must be added with session_id as the key value
// Retrieve
$ Name = $ _ SESSION ['name'];
Echo "name = $ name ";
Echo "sessionid =". session_id ();
?>

If you do not support the memcache php. memcache. dll extension, you can use the native memcached_client.php function, but the efficiency is low.


If the administrator does not allow us to modify php. ini file, how do we implement the memcached function of session entry, we dynamically modify php through the ini_set function. ini configuration only applies to this page. other php pages are not affected and the configuration file is not modified.

Ini_set ("session. save_handler", "memcache ");

Ini_set ("session. save_path", "tcp: // 127.0.0.1: 11211 ");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.