create database test;
use test;
create table TBL_SESSION (
SESSION_ID varchar(50) NOT NULL,
SESSION_DATA text,
CRA¥EATE_DATE int(10),
PRIMARY KEY (SESSION_ID));
class MySessionHandler implements SessionHandlerInterface { function close(){ return true; } function destroy($session_id) { $db = get_db(); if($stmt = $db->prepare("DELETE FROM TBL_SESSION WHERE SESSION_ID = ?")){ $stmt->bind_param("s", $session_id); $stmt->execute(); $stmt->close(); $stmt = null; } $db->close(); $db = null; return true; } function open($save_path, $name) { return true; } function read($session_id) { $session_data = ""; $db = get_db(); if($stmt = $db->prepare("SELECT SESSION_DATA FROM TBL_SESSION WHERE SESSION_ID = ?")){ $stmt->bind_param("s", $session_id); $stmt->bind_result($session_data); $stmt->execute(); $stmt->fetch(); $stmt->close(); $stmt = null; } $db->close(); $db = null; if(is_null($session_data)){ $session_data = ""; } return $session_data; } function write($session_id, $sesion_data){ $affect_rows = 0; $create_date = time(); $db = get_db(); if($stmt = $db->prepare("INSERT INTO TBL_SESSION (SESSION_ID, SESSION_DATA, CREATE_DATE) VALUES(?, ?, ?) ON DUPLICATE KEY UPDATE SESSION_DATA = ?, CREATE_DATE = ?")){ $stmt->bind_param("ssisi", $session_id, $session_data, $create_date, $session_data, $create_date); $stmt->execute(); $affected_rows = $stmt->affected_rows; $stmt->close(); $stmt = null; } $db->close(); $db = null; return $affected_rows ? true : false; } } function get_db(){ return new mysqli('localhost:3306', 'user', 'password', 'db_name'); } session_set_save_handler(new MySessionHandler(), true); session_start(); $_SESSION['data'] = 0;
Railsは全てのセッション情報をcookieに保存