存档

2014年2月 的存档

php将session保存在memcached中

2014年2月28日 评论已被关闭

1.首先安装memcached或者memcache软件
参考:wamp 安装 memcache/memcached详细说明
2.安装php_memcache.dll扩展
3.设置session保存在memcached
方法一:
修改php.ini文件

session.save_handler = memcache
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");

测试程序
1.session.php

<?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();//5877fbfcaea379ada0cc0dbba80615a6

?>

2.test.php

<?php
// memcache test code
$memcache = new Memcache; //创建一个memcache对象  
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect"); //连接Memcached服务器  

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

?>

参考资料
1.http://www.dotdeb.org/2008/08/25/storing-your-php-sessions-using-memcached/

2.Warning: ini_set() [function.ini-set]: Cannot find save handler 'memcache'错误原因
http://zeroq.me/php/2011/07/27/phpe4bdbfe794a8memcachee69da5e4bf9de5ad98sessione79a84e9858de7bdaee8aeb0e5bd95/
3.http://www.dotdeb.org/2008/08/25/storing-your-php-sessions-using-memcached/
4.http://www.phpv.net/html/1542.html

分类: php 标签:

使用php将session保存到MySql数据库中

2014年2月28日 评论已被关闭

php中session默认的存储方式是硬盘,php也可以改变默认的存储方式。
主要使用到session_set_save_handler方法,下面分享下如何将session保存到MySql数据库中的具体代码。

1.建session表

CREATE TABLE `session` (
  `sessionid` varchar(128) NOT NULL,
  `uid` int(11) NOT NULL,
  `data` mediumblob NOT NULL,
  `timestamp` int(11) NOT NULL,
  `ip` varchar(15) NOT NULL,
  PRIMARY KEY  (`sessionid`),
  KEY `time_session` (`timestamp`,`sessionid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


uid 是保留字段

2.自定义session类

<?php
class CustomSession{
	private static $db_host="localhost";
	private static $db_user="root";
	private static $db_password="";
	private static $database="session";
	
	private $conn;
	
	public static function getInstance(){
		static $instance=null;
		if($instance==null){
			$instance=new CustomSession();
		}
		
		return $instance;
	}
	
	public function __construct(){
		session_set_save_handler(
		array($this,"open"),
		array($this,"close"),
		array($this,"read"),
		array($this,"write"),
		array($this,"destroy"),
		array($this,"gc")
		);
	}
	
	public function __destruct(){
		session_write_close();
	}
	
	public function open(){
		$this->conn=mysql_connect(CustomSession::$db_host,CustomSession::$db_user,CustomSession::$db_password);
		mysql_select_db(CustomSession::$database,$this->conn);
	}
	
	public function close(){
		
		mysql_close($this->conn);
	}
	
	public function read($id){
		$escaped_id=mysql_escape_string($id);
		$res=$this->query("select * from `session` where `sessionid`='$escaped_id'");
		if($row=mysql_fetch_assoc($res)){
			$this->query("update `session` set `timetamp`=UTC_TIMESTAMP() where `sessionid`='$escaped_id'");
			return $row['data'];
		}
		return "";
	}
	
	public function write($id,$data){
		$query="replace into `session` (`sessionid`,`data`,`ip`,`timestamp`) values ('%s','%s','%s',UNIX_TIMESTAMP(UTC_TIMESTAMP()))";
		$this->query(sprintf($query,mysql_escape_string($id),mysql_escape_string($data),$_SERVER["REMOTE_ADDR"]));
	}
	
	public function destroy($id){
		$escaped_id=mysql_escape_string($id);
		$res=$this->query("delete from `session` where `id`='$escaped_id'");
		return (mysql_affected_rows($res)==1);
	}
	
	public function gc($lifetime){
		$this->query("delete from `session` where UNIX_TIMESTAMP(UTC_TIMESTAMP())-`timestamp` > $lifetime");
	}
	
	public function query($query){
		$res=mysql_query($query,$this->conn);
		return $res;
	}
}

?>

3.测试程序

<?php
include('./CustomSession.class.php');

CustomSession::getInstance();

session_start();

$_SESSION['username']='feng';

print_r($_SESSION);

?>

运行测试程序后,查看数据库可以发现session表中已经增加了session记录。

具体参考资料:
http://cn2.php.net/session_set_save_handler
http://cn2.php.net/manual/zh/class.sessionhandlerinterface.php
https://www.stanford.edu/dept/its/communications/webservices/wiki/index.php/How_to_use_MySQL-based_sessions#Add_session_handling_code_to_your_application
http://avedo.net/402/mysql-based-session-mangagement-in-php/

分类: php 标签:

div css 实现半透明效果

2014年2月25日 评论已被关闭

css

.project{ position:relative;width:288px; height:183px;}
.project .box{ position:absolute; top:0px; left:0px; display:block; width:288px; height:183px; color:#FFF; padding:10px; opacity: 0.8;filter: alpha(opacity=80);-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=80)";}

html

<div class="project">
<div class="box">
div css 实现半透明效果测试文字
</div>
</div>

分类: css 标签:


标签

ajax apache array array_slice CentOS checkbox composer cookie csv curl date DATE_FORMAT excel FCKEditor file_get_contents laravel Magento Certified Developer mb_substr memcache Memcached MySQL String Functions Netbeans openssl php面试题 preg_match_all preg_replace QQ登录 rewrite SaeStorage serialize session setTimeout Sphinx spl Storage strtotime ueditor vsftp vsftpd Zend zip 分割字符串 图片处理 新浪云 正则表达式

存档