How to Save Session Data to Memcache and Memcache Lifespan

Saving Session Data to Memcache

Session data is, by default, saved in file files

But we can modify PHP configuration to save it elsewhere

(1). Open D:/lamp/php/php.ini. session.save_handler = files is enabled; comment it out

 

Session content save path: add the line underlined in red

After modification, let’s test it

Session.php

//
<?php

session_start();

$_SESSION['name']='whj';

?>

Get_session.php

<?php

session_start();

$name=$_SESSION['name'];

echo $name;

?>

Outputs whj — correct

(2). However, when retrieving session variables, it is no longer the name attribute; instead, they are stored by session_id

session_id: When the browser accesses the server, the server assigns a session_id to the browser, and then uses the session_id to find the corresponding values

Example:

 

session_start();

$_SESSION['age']='whjwhj';

$sess_id=session_id();

var_dump($sess_id);

// Running this outputs string(26) "dmkppdo0qhbkq099fo608an383". In telnet, running get dmkppdo0qhbkq099fo608an383 yields age|s:6:"whjwhj";

 

What if you do not have permission to modify PHP configuration?

ini_set() can set certain PHP configurations within a PHP file

Security concern: Anyone can now access my memcache. Others accessing my telnet 192.168.2.200 can also access it.

Solution: Firewall

 

The above session usage modified the php.ini configuration file. Now, without modifying the configuration file, simply add the following at the beginning of the file:

ini_set('session.save_handler','memcache');

ini_set('session.save_path','tcp://127.0.0.1:11211'); This tells it to save session data to memcache

Example:

//

<?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 Lifespan:

Restarting memcached or rebooting the operating system will cause all data to disappear. Additionally, when the memory capacity reaches a specified value, unused caches are automatically deleted based on the LRU (Least Recently Used) algorithm.

If expire is set to 0, it means never expire (until machine reboot or service restart)

 http://blog.csdn.net/whjwhja6/article/details/9182671

Leave a Comment

Your email address will not be published.