php -vPHP 5.4.42 (cli) (built: Jul 1 2015 22:46:17) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
php -i | grep -i "thread"Thread Safety => enabled
今はphpはthreadセーフなのか。。。
てことはApacheのmpmは何でもいいですね。
php -vPHP 5.4.42 (cli) (built: Jul 1 2015 22:46:17) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
php -i | grep -i "thread"Thread Safety => enabled
./configure \ --prefix=/usr/local/apache2.4.12 \ --enable-so \ --enable-ssl \ --enable-mods-shared=all \ --with-expat=builtin \ --with-included-apr \ --enable-proxy \ --enable-proxy-http \ --enable-proxy-ajp \ --with-mpm=prefork \ --enable-rewrite
StartServers:最初に起動する子プロセスの数StartServers 5 MinSpareServers 10 MaxSpareServers 30 MaxRequestWorkers 1000 ServerLimit 1000 MaxConnectionsPerChild 30000
httpd -V
<?php ini_set('session.gc_maxlifetime', 100); ini_set('session.gc_probability', 100); ini_set('session.gc_divisor', 100); session_set_cookie_params(100); class Session implements SessionHandlerInterface { private $_db; private $_fp; public function __construct(){ try{ $this->_fp = fopen("log.txt", "a+"); fwrite($this->_fp, "construct: "."\n"); $this->_db = mysql_connect('localhost', 'root', ''); $db_selected = mysql_select_db('development'); if(!$db_selected){ throw new Exception("SQLException DBの接続に失敗しました"); } if(isset($_COOKIE["PHPSESSID"])){ session_id($_COOKIE["PHPSESSID"]); } }catch(Exception $e){ fwrite($this->_fp, $e->getMessage()."\n"); throw $e; } } public function open($save_path, $session_id) { fwrite($this->_fp, "open: "."\n"); return true; } public function read($session_id) { try{ $session_id = mysql_real_escape_string($session_id); $select_data = "SELECT data " . "FROM sessions " . "WHERE session_id = '" . $session_id . "'"; fwrite($this->_fp, "read: " . $select_data . "\n"); $result = mysql_query($select_data); if(!$result){ throw new Exception("SQLException dataの取得に失敗しました"); } $row = mysql_fetch_row($result); return $row[0]; }catch(Exception $e){ fwrite($this->_fp, $e->getMessage()."\n"); throw $e; } } public function write($session_id, $session_data) { try{ $count_sessions = "SELECT COUNT(*) FROM sessions WHERE session_id= '" . $session_id . "'"; $result = mysql_query($count_sessions); if(!$result){ throw new Exception("SQLException dataのカウントに失敗しました"); } $row = mysql_fetch_row($result); $count = intval($row[0]); if($count > 0){ $update_sessions = "UPDATE sessions " . "SET data='" . $session_data . "'," . "updated_at = now() " . "WHERE session_id = '" . $session_id . "'"; fwrite($this->_fp, "write: " . $update_sessions . "\n"); $result = mysql_query($update_sessions); if(!$result){ throw new Exception("SQLException dataの更新に失敗しました"); } }else{ $insert_sessions = "INSERT INTO sessions(session_id, " . " data," . " created_at)" . "VALUES(" . "'" . $session_id . "'," . "'" . $session_data . "'," . " now())"; fwrite($this->_fp, "write: " . $insert_sessions . "\n"); $result = mysql_query($insert_sessions); if(!$result){ throw new Exception("SQLException dataの登録に失敗しました"); } } }catch(Exception $e){ fwrite($this->_fp, $e->getMessage()."\n"); throw $e; } return true; } public function close() { fwrite($this->_fp, "close: "."\n"); mysql_close($this->_db); return true; } public function destroy($session_id) { try{ $session_id = mysql_real_escape_string($session_id); $delete_sessions = "DELETE FROM sessions WHERE session_id = '" . $session_id . "'"; fwrite($this->_fp, "destroy: " . $delete_sessions . "\n"); $result = mysql_query($delete_sessions); if(!$result){ throw new Exception("SQLException sessionsの削除に失敗しました"); } }catch(Exception $e){ fwrite($this->_fp, $e->getMessage()."\n"); throw $e; } return true; } public function gc($maxlifetime) { try{ $gc_sessions = "DELETE " . "FROM sessions " . "WHERE updated_at < current_timestamp + '-" . $maxlifetime . " secs' " . " OR (created_at < current_timestamp + '-" . $maxlifetime . " secs' AND updated_at IS NULL)"; fwrite($this->_fp, "gc: " . $gc_sessions . "\n"); $result = mysql_query($gc_sessions); if(!$result){ throw new Exception("SQLException gcに失敗しました"); } }catch(Exception $e){ fwrite($this->_fp, $e->getMessage()."\n"); throw $e; } return true; } }db_test.php
<?php require_once 'Session.php'; try{ $handler = new Session(); session_set_save_handler($handler, true); session_start(); $_SESSION["box"] = "test"; var_dump($_SESSION); }catch(Exception $e){ echo $e->getMessage(); }db_test2.php
<?php try{ require_once 'Session.php'; $handler = new Session(); session_set_save_handler($handler, true); session_start(); $_SESSION["box"] = "box"; var_dump($_SESSION); session_destroy(); }catch(Exception $e){ echo $e->getMessage(); }
CREATE TABLE `sessions` ( `id` int(11) NOT NULL AUTO_INCREMENT, `session_id` char(32) NOT NULL, `data` text, `created_at` datetime DEFAULT NULL, `updated_at` datetime DEFAULT NULL, PRIMARY KEY (`id`), KEY `idx_session_id` (`session_id`), KEY `idx_created_at` (`created_at`), KEY `idx_updated_at` (`updated_at`) )DBSession.php
<?php ini_set('session.save_handler', 'user'); ini_set('session.gc_maxlifetime', 100); ini_set('session.gc_probability', 100); ini_set('session.gc_divisor', 100); session_set_save_handler('open', 'close', 'read', 'write', 'destroy', 'gc'); session_set_cookie_params(100); define('HOST', "localhost"); define('USER', "root"); define('PASSWORD', ""); define('DATABASE', "development"); if(isset($_COOKIE["PHPSESSID"])){ session_id($_COOKIE["PHPSESSID"]); } function open(){ return true; } function close(){ return true; } function read($session_id){ $rtn = ""; $db = new mysqli(HOST, USER, PASSWORD, DATABASE); $session_id = mysql_real_escape_string($session_id); $select_data = "SELECT data " . "FROM sessions " . "WHERE session_id = '" . $session_id . "'"; $fp = fopen("log.txt", "a+"); fwrite($fp, "read: " . $select_data . "\n"); $rs = $db->query($select_data); if($rs){ while ($row = $rs->fetch_assoc()) { $rtn = $row['data']; } } function write($session_id, $data){ $rtn = false; $db = new mysqli(HOST, USER, PASSWORD, DATABASE); $session_id = mysql_real_escape_string($session_id); $data = mysql_real_escape_string($data); $select_sessions = "SELECT * FROM sessions WHERE session_id='" . $session_id . "'"; $fp = fopen("/var/www/html/sessiontest/log.txt", "a+"); fwrite($fp, "write: " . $select_sessions . "\n"); $rs = $db->query($select_sessions); if($rs->fetch_row() > 0){ $update_sessions = "UPDATE sessions " . "SET data = '" . $data . "'," . "updated_at = NOW() " . "WHERE session_id = '" . $session_id . "'"; $rs = $db->query($update_sessions); }else{ $insert_sessions = "INSERT INTO sessions(session_id, data, created_at)" . "VALUES('" . $session_id . "','" . $data . "',NOW())"; $rs = $db->query($insert_sessions); var_dump($rs); } $rtn = true; return $rtn; } return $rtn; } function destroy($session_id){ $db = new mysqli(HOST, USER, PASSWORD, DATABASE); $delete_sessions = "DELETE FROM sessions WHERE session_id = '" . $session_id . "'"; $fp = fopen("/var/www/html/sessiontest/log.txt", "a+"); fwrite($fp, "destroy: " . $delete_sessions . "\n"); $db->query($delete_sessions); return true; } function gc($maxlifetime){ $rtn = false; $db = new mysqli(HOST, USER, PASSWORD, DATABASE); $gc_sessions = "DELETE " . "FROM sessions " . "WHERE updated_at < current_timestamp + '-" . $maxlifetime . " secs' " . " OR (created_at < current_timestamp + '-" . $maxlifetime . " secs' AND updated_at IS NULL)"; $db->query($gc_sessions); $fp = fopen("/var/www/html/sessiontest/log.txt", "a+"); fwrite($fp, "gc: " . $gc_sessions . "\n"); $rtn = true; return $rtn; }
<?php require_once 'DBSession.php'; session_start(); $_SESSION["box"] = "test"; var_dump($_SESSION);
<?php require_once 'DBSession.php'; session_start(); var_dump($_SESSION); session_destroy();