Php method for saving session in memcached

Source: Internet
Author: User

1. First install memcached or memcache software
See wamp for detailed instructions on installing memcache/memcached.
2. Install php_memcache.dll extension
3. Set the session to be saved in memcached

Method 1:

Modify the php. Ini file

The code is as follows: Copy code

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

Method 2:

Dynamic settings in the program

The code is as follows: Copy code

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

Test Procedure

1. session. php

The code is as follows: Copy code

<? Php
Ini_set ("session. save_handler", "memcache ");
Ini_set ("session. save_path", "tcp: // 127.0.0.1: 11211 ");

Session_start ();
If (! Isset ($ _ SESSION ['username']) {
$ _ SESSION ['username'] = 'phpff. Com ';
}

Print session_id (); // 5877fbfcaea?ada0cc0dbba80615a6

?>

2. test. php

The code is as follows: Copy code

<? Php
// Memcache test code
$ Memcache = new Memcache; // Create a memcache object
$ Memcache-> connect ('192. 0.0.1 ', 127) or die ("cocould not connect"); // connect to the Memcached server

Print_r ($ memcache-> get ('5d3d79d7500f5836033e9019b3eefc6f'); // username | s: 9: "phpff.com ";

?>

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.