Three ways to configure the session using Memcache Storage in PHP _php tutorial

Source: Internet
Author: User
Tags session id
The following is a simple PHP project distributed deployment, session synchronization of one of the scenarios, using Memcache to store the session. And summed up three ways to configure, the need for friends can refer to the following

1, directly modify the php.ini configuration file

The code is as follows:

Session.save_handler = memcache//Set session is stored in Memcache

Memcache.hash_strategy = "consistent"//set Memcache hash algorithm

Session.save_path = "tcp://127.0.0.100:11211"//Set Session storage location, multiple memcache separated by commas, for example: tcp://127.0.0.1:11211,tcp:// 127.0.0.1:12000

2. Use the. htaccess file configuration under the directory

The code is as follows:

Php_value Session.save_handler "Memcache"

Php_value Session.save_path "tcp://127.0.0.1:11211"

Description: This is only for Apache, the current use of nginx is more, it is not recommended this way.

3. Modify the configuration in the PHP file of the project

Copy the code code as follows: Ini_set ("Session.save_handler", "memcache");

Ini_set ("Session.save_path", "tcp://127.0.0.100:11211");

4. Test examples

The code is as follows:

Test session Read is normal

Session_Start ();

$_session[' username '] = "jb51.net";

Echo session_id ();

Read the session from the Memcache

$m = new Memcache ();

$m->connect (' localhost ', 11211);

Or so

$mem->addserver ("127.0.0.1", 11211) or Die ("Can ' t add Memcache server 127.0.0.1:12000");

Get data based on session_id

Natively

$session = $m->get (session_id ()); session_id:d527b6f983bd5e941f9fff318a31206b

Another server, known as session ID

$session = $m->get ("d527b6f983bd5e941f9fff318a31206b");

echo $session. "
"; Will get the data: username|s:16: "Pandao", and parse it to get the corresponding value

Echo session_id (). "
";

Exit

Above is just some simple explanation, actually said humorous digression, generally, do distributed, that certainly has the server permission, so recommend the first kind.

http://www.bkjia.com/PHPjc/750260.html www.bkjia.com true http://www.bkjia.com/PHPjc/750260.html techarticle The following is a simple PHP project distributed deployment, session synchronization of one of the scenarios, using Memcache to store the session. and summed up three kinds of configuration methods, the need for friends can refer to the following ...

  • 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.