Ubuntu server 11.04安装memcache及php使用memcache来存储session的方法,11.04memcache
Ubuntu server 11.04安装memcache及php使用memcache来存储session的方法,11.04memcache
本文实例讲述了Ubuntu server 11.04安装memcache及php使用memcache来存储session的方法。分享给大家供大家参考,具体如下:
1、首先安装memcache服务端:
sudo apt-get install memcached
安装完成后系统 自动启动了 memcached服务占用11211端口
如需重新配置11211端口的服务 需要关闭已开启的memcached服务
手动启动:
memcached -d -m 128 -p 11211 -u memcache
这里需要说明一下memcached服务的启动参数:
-p 监听的端口
-l 连接的IP地址, 默认是本机
-d start 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u 以的身份运行 (仅在以root运行的时候有效)
-m 最大内存使用,单位MB。默认64MB
-M 内存耗尽时返回错误,而不是删除项
-c 最大同时连接数,默认是1024
-f 块大小增长因子,默认是1.25-n 最小分配空间,key+value+flags默认是48
-h 显示帮助
2、安装PHP Memecache 客户端
$ sudo apt-get install php5-memcache
重启web服务器
测试memcache代码:
<?php $mem = new Memcache; //创建Memcache对象 $mem->connect("127.0.0.1", 11211); //连接Memcache服务器 $val = "这是一个Memcache的测试."; $key = md5($val); if(($k = $mem->get($key))){ //判断是否获取到指定的key echo 'from cache:'.$k; } else { echo 'normal'; //这里我们在实际使用中就需要替换成查询数据库并创建缓存. $mem->set($key, $val, 0, 120); //增加插入一条缓存,缓存时间为120s }
用memcache存储session
一般地, Session 是以文本文件形式存储在服务器端的。如果使用 Seesion,或者该 PHP 文件要调用 Session 变量,那么就必须在调用 Session 之前启动它,使用 session_start() 函数。其它都不需要你设置了,PHP 自动完成 Session 文件的创建。其默认 Session 的存放路径是服务器的系统临时文件夹。
但是如果碰到大数据量的Sesstion的时候, 使用基于文件的Session存取瓶颈可能都是在磁盘IO操作上,现在利用Memcached来保存Session数据,直接通过内存的方式,效率自然能够提高不少。 在读写速度上会比 files 时快很多,而且在多个服务器需要共用 session 时会比较方便,将这些服务器都配置成使用同一组 memcached 服务器就可以,减少了额外的工作量。
其缺点是 session 数据都保存在 memory 中,一旦宕机,数据将会丢失。但对 session 数据来说并不是严重的问题。
如何用 memcached 来存储 session呢?以下是基本的配置步骤:
1. 安装 memcached (略过,不清楚的筒子可以查看前面一篇文章:http://www.bkjia.com/article/85510.htm)
在 phpinfo 输出中的 "Registered save handlers" 会有 "files user sqlite"。
2. 修改配置文件,
①. 在 php.ini 中全局设置(* 需要重启服务器)
session.save_handler = memcache session.save_path = "tcp://127.0.0.1:11211"
②. 或者某个目录下的 .htaccess :
php_value session.save_handler "memcache" php_value session.save_path "tcp://127.0.0.1:11211"
③. 也可以在某个一个应用中:
ini_set("session.save_handler", "memcache");
ini_set("session.save_path", "tcp://127.0.0.1:11211");
注:使用多个 memcached server 时用逗号","隔开,并且和 Memcache::addServer() 文档中说明的一样,可以带额外的参数"persistent"、"weight"、"timeout"、"retry_interval" 等等,类似这样的:
"tcp://host:port?persistent=1&weight=2,tcp://host2:port2" 。
3. 启动 memcached
复制代码 代码如下:memcached -d -m 10 -u root -l 127.0.0.1 -p 11211 -c 256 -P /tmp/memcached.pid
4.测试 创建一个 session
<?php //set_session.php session_start(); if (!isset($_SESSION['admin'])) { $_SESSION['admin'] = 'wan'; } print $_SESSION['admin']; print "/n"; print session_id(); ?>
5. 用 sessionid 去 memcached 里查询一下
<?php //get_session.php $mem = new Memcache; $mem->connect("127.0.0.1", 11211); var_dump($mem->get('0935216dbc0d721d629f89efb89affa6')); ?>
复制代码 代码如下:[root@localhost html]# /usr/local/webserver/php/bin/php -f get_session.php
输出结果:
string(16) "admin|s:3:"wan";"
证明 session 正常工作。
再次深入 多域名网站利用MEMCACHE方式共享SESSION数据
通过了解 SESSION 的工作原理,我们可以发现,在默认情况下,各个服务器会各自分别对同一个客户端产生 SESSION ID,如对于同一个用户浏览器,A 服务器产生的 SESSION ID 是 30de1e9de3192ba6ce2992d27a1b6a0a,而 B 服务器生成的则是 c72665af28a8b14c0fe11afe3b59b51b。另外,PHP 的 SESSION 数据都是分别保存在本服务器的文件系统中。
确定了问题所在之后,就可以着手进行解决了。想要共享 SESSION 数据,那就必须实现两个目标:一个是各个服务器对同一个客户端产生的 SESSION ID 必须相同,并且可通过同一个 COOKIE 进行传递,也就是说各个服务器必须可以读取同一个名为 PHPSESSID 的 COOKIE;另一个是 SESSION 数据的存储方式/位置必须保证各个服务器都能够访问到。简单地说就是多服务器共享客户端的 SESSION ID,同时还必须共享服务器端的 SESSION 数据。
第一个目标的实现其实很简单,只需要对 COOKIE 的域(domain)进行特殊地设置即可,默认情况下,COOKIE 的域是当前服务器的域名/IP 地址,而域不同的话,各个服务器所设置的 COOKIE 是不能相互访问的,如 www.aaa.com 的服务器是不能读写 www.bbb.com 服务器设置的 COOKIE 的。这里我们所说的同一网站的服务器有其特殊性,那就是他们同属于同一个一级域,如:tieba.xiaoyuan.com 和 www.xiaoyuan.com 都属于域 .xiaoyuan.com,那么我们就可以设置 COOKIE 的域为 .xiaoyuan.com,这样 tieba.xiaoyuan.com、www.xiaoyuan.com 等等都可以访问此 COOKIE。PHP 代码中的设置方法如下:
<?php ini_set('session.cookie_domain', 'xiaoyuan.com'); ?>
这样各个服务器共享同一客户端 SESSION ID 的目的就达到了。
于是 可以在 a.domain.com 下
session.php
<?php ini_set('session.cookie_domain', 'domain.com'); session_start(); if (!isset($_SESSION['admin'])) { $_SESSION['admin'] = 'wan'; } print $_SESSION['admin']; print "\n"; print session_id();
在b.domain.com下
session2.php
<?php ini_set('session.cookie_domain', 'domain.com'); session_start(); echo $_SESSION['admin'];
希望本文所述对大家Ubuntu平台上的php程序设计有所帮助。

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
