{"id":4597,"date":"2010-07-19T12:00:03","date_gmt":"2010-07-19T03:00:03","guid":{"rendered":"http:\/\/fukata.org\/?p=4597"},"modified":"2017-08-11T22:58:50","modified_gmt":"2017-08-11T22:58:50","slug":"codeigniter-ci_session-extends-use-php-session","status":"publish","type":"post","link":"https:\/\/blog.fukata.org\/archives\/4597\/","title":{"rendered":"[CodeIgniter]CI_Session\u3092\u62e1\u5f35\u3057\u3066PHP\u6a19\u6e96Session\u306b\u5bfe\u5fdc\u3057\u3066\u307f\u305f"},"content":{"rendered":"

CodeIgniter\u304c\u63d0\u4f9b\u3057\u3066\u3044\u308b\u30bb\u30c3\u30b7\u30e7\u30f3\u30af\u30e9\u30b9\u306f\u3001Cookie\u306b\u5168\u3066\u4fdd\u5b58\u3059\u308b\u304b\u3001\u30e6\u30fc\u30b6\u304c\u8ffd\u52a0\u3057\u305f\u30c7\u30fc\u30bf\u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u4fdd\u5b58\u3057\u3066\u304a\u3044\u3066\u3001\u30ad\u30fc\u306fCookie\u306b\u4fdd\u5b58\u3059\u308b\u304b\u306e2\u7a2e\u985e\u3092\u9078\u629e\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n

\u305f\u3060\u3001PHP\u6a19\u6e96\u306e\u30bb\u30c3\u30b7\u30e7\u30f3\u6a5f\u69cb\u3092\u4f7f\u3046\u3053\u3068\u306b\u306f\u5bfe\u5fdc\u3057\u3066\u304a\u3089\u305a\u3001\u3069\u3046\u3057\u3066\u3082\u5229\u7528\u3057\u305f\u3044\u5834\u5408\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u30d9\u30bf\u3067\u8a18\u8ff0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n

\r\nsession_start();\r\n$_SESSION['hoge'] = 'foo';\r\n<\/pre>\n

\u4e0a\u8a18\u3067\u3082\u57fa\u672c\u7684\u306b\u52d5\u304f\u3068\u306f\u601d\u3044\u307e\u3059\u304c\u3001\u30bb\u30c3\u30b7\u30e7\u30f3\u6a5f\u69cb\u3092\u5207\u308a\u66ff\u3048\u308b\u969b\u306e\u30b3\u30b9\u30c8\u304c\u9ad8\u3044\u306e\u3067\u3001\u30b3\u30a2\u30af\u30e9\u30b9\u306eCI_Session\u3092\u62e1\u5f35\u3057\u3066PHP\u6a19\u6e96\u30bb\u30c3\u30b7\u30e7\u30f3\u6a5f\u69cb\u3082\u9078\u629e\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n

system\/application\/libraries\/XC_Session.php<\/h3>\n
\r\n< ?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r\n\/**\r\n * PHP\u6a19\u6e96Session\u6a5f\u69cb\u3092\u5229\u7528\u3067\u304d\u308b\r\n * \r\n * @author Tatsuya Fukata\r\n *\r\n *\/\r\nclass XC_Session extends CI_Session {\r\n\t\/** PHP\u6a19\u6e96\u306eSession\u6a5f\u80fd\u3092\u4f7f\u7528\u3059\u308b\u304b *\/\r\n\tprivate $sess_use_php_session = false;\r\n\r\n\t\/** \u521d\u671f\u5316\u6642\u306b\u8aad\u307f\u8fbc\u3080\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u306e\u30ad\u30fc\u4e00\u89a7 *\/\r\n\tprivate static $INIT_CONFIG_KEYS = array(\r\n\t\t'sess_use_php_session', \r\n\t\t'sess_encrypt_cookie', \r\n\t\t'sess_use_database', \r\n\t\t'sess_table_name', \r\n\t\t'sess_expiration', \r\n\t\t'sess_match_ip', \r\n\t\t'sess_match_useragent', \r\n\t\t'sess_cookie_name', \r\n\t\t'cookie_path', \r\n\t\t'cookie_domain', \r\n\t\t'sess_time_to_update', \r\n\t\t'time_reference', \r\n\t\t'cookie_prefix', \r\n\t\t'encryption_key',\r\n\t);\r\n\t\r\n\t\/**\r\n\t * \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\r\n\t * @param Array $params\r\n\t *\/\r\n\tpublic function __construct($params = array()) {\r\n\t\t$this->CI =& get_instance();\r\n\t\tforeach (self::$INIT_CONFIG_KEYS as $key) {\r\n\t\t\t$this->$key = (isset($params[$key])) ? $params[$key] : $this->CI->config->item($key);\r\n\t\t}\r\n\r\n\t\tif ($this->use_not_php_session()) {\r\n\t\t\tparent::__construct($params);\r\n\t\t} else {\r\n\t\t\t$this->initialize($params);\r\n\t\t}\r\n\t}\r\n\r\n\t\/**\r\n\t * PHP\u6a19\u6e96Session\u5229\u7528\u6642\u306e\u521d\u671f\u5316\r\n\t * @param Array $params\r\n\t * @return void\r\n\t *\/\r\n\tprotected function initialize($params) {\r\n\t\tsession_start();\r\n\t\t\r\n\t\t\/\/ XXX \u30b9\u30fc\u30d1\u30af\u30e9\u30b9\u306eprivate\u30e1\u30bd\u30c3\u30c9\u306e\u305f\u3081\u3001\u500b\u5225\u5b9a\u7fa9\r\n\t\t\/\/ Set the \"now\" time.  Can either be GMT or server time, based on the\r\n\t\t\/\/ config prefs.  We use this to set the \"last activity\" time\r\n\t\t$this->now = $this->_get_time();\r\n\t\r\n\t\t\/\/ Set the session length. If the session expiration is\r\n\t\t\/\/ set to zero we'll set the expiration two years from now.\r\n\t\tif ($this->sess_expiration == 0) {\r\n\t\t\t$this->sess_expiration = (60*60*24*365*2);\r\n\t\t}\r\n\t\t\r\n\t\t\/\/ \u30bb\u30c3\u30b7\u30e7\u30f3\u304c\u65e2\u306b\u5b58\u5728\u3059\u308c\u3070\u66f4\u65b0\u3001\u672a\u4f5c\u6210\u306e\u5834\u5408\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3092\u4f5c\u6210\u3059\u308b\r\n\t\tif (!$this->sess_read()) {\r\n\t\t\t$this->sess_create();\r\n\t\t} else {\r\n\t\t\t$this->sess_update();\r\n\t\t}\r\n\t\t\r\n\t\t\/*\r\n\t\t * XXX \u30b9\u30fc\u30d1\u30af\u30e9\u30b9\u306eprivate\u30e1\u30bd\u30c3\u30c9\u306e\u305f\u3081\u3001\u500b\u5225\u5b9a\u7fa9\r\n\t\t * \u30e1\u30bd\u30c3\u30c9\u300c_flashdata_sweep\u300d\u300c_flashdata_mark\u300d\u300c_sess_gc\u300d\u306f\r\n\t\t * CI_Session\u306b\u30d7\u30e9\u30a4\u30d9\u30fc\u30c8\u30e1\u30bd\u30c3\u30c9\u3092\u610f\u8b58\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\r\n\t\t * \u672c\u6765\u3067\u3042\u308c\u3070\u3001XC_Session\u3067\u518d\u5b9a\u7fa9\u3057\u305f\u3044\u3068\u3053\u308d\u3002\r\n\t\t *\/\r\n\t\t\/\/ Delete 'old' flashdata (from last request)\r\n\t   \t$this->_flashdata_sweep();\r\n\r\n\t\t\/\/ Mark all new flashdata as old (data will be deleted before next request)\r\n\t   \t$this->_flashdata_mark();\r\n\r\n\t\t\/\/ Delete expired sessions if necessary\r\n\t\t$this->_sess_gc();\r\n\r\n\t\t\r\n\t}\r\n\r\n\t\/**\r\n\t * Override\r\n\t *\/\r\n\tpublic function sess_read() {\r\n\t\tif ($this->use_not_php_session()) {\r\n\t\t\treturn parent::sess_read();\r\n\t\t}\r\n\r\n\t\tif (count($_SESSION) == 0) {\r\n\t\t\treturn false;\r\n\t\t}\r\n\t\t\r\n\t\t\/\/ Is the session current?\r\n\t\tif (($_SESSION['last_activity'] + $this->sess_expiration) < $this->now) {\r\n\t\t\t$this->sess_destroy();\r\n\t\t\treturn FALSE;\r\n\t\t}\r\n\t\r\n\t\t\/\/ Does the IP Match?\r\n\t\tif ($this->sess_match_ip == TRUE AND $_SESSION['ip_address'] != $this->CI->input->ip_address()) {\r\n\t\t\t$this->sess_destroy();\r\n\t\t\treturn FALSE;\r\n\t\t}\r\n\r\n\t\t\/\/ Does the User Agent Match?\r\n\t\tif ($this->sess_match_useragent == TRUE AND trim($_SESSION['user_agent']) != trim(substr($this->CI->input->user_agent(), 0, 50))) {\r\n\t\t\t$this->sess_destroy();\r\n\t\t\treturn FALSE;\r\n\t\t}\r\n\r\n\t\t$this->userdata = $_SESSION;\r\n\t\treturn true;\r\n\t}\r\n\r\n\t\/**\r\n\t * Override\r\n\t *\/\r\n\tpublic function sess_create() {\r\n\t\tif ($this->use_not_php_session()) {\r\n\t\t\tparent::sess_create();\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\t\/\/ \u521d\u671f\u30c7\u30fc\u30bf\u3092\u8a2d\u5b9a\r\n\t\t$_SESSION = array(\r\n\t\t\t'session_id' => session_id(),\r\n\t\t\t'ip_address' => $this->CI->input->ip_address(),\r\n\t\t\t'user_agent' => substr($this->CI->input->user_agent(), 0, 50),\r\n\t\t\t'last_activity' => $this->now\r\n\t\t);\r\n\r\n\t\t$this->userdata = $_SESSION;\r\n\t\t$this->sess_write();\r\n\t}\r\n\r\n\t\/**\r\n\t * Override\r\n\t *\/\r\n\tpublic function sess_write() {\r\n\t\tif ($this->use_not_php_session()) {\r\n\t\t\tparent::sess_write();\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\t$_SESSION = $this->userdata;\r\n\t}\r\n\r\n\t\/**\r\n\t * Override\r\n\t *\/\r\n\tpublic function sess_update() {\r\n\t\tif ($this->use_not_php_session()) {\r\n\t\t\tparent::sess_update();\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\t\/\/ We only update the session every five minutes by default\r\n\t\tif (($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now) {\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\t\/\/ \u65b0\u3057\u3044\u30bb\u30c3\u30b7\u30e7\u30f3ID\u306e\u751f\u6210\u3057\u3001\u8a2d\u5b9a\r\n\t\tsession_regenerate_id(true);\r\n\t\t$this->userdata['session_id'] = session_id();\r\n\t\t$this->userdata['last_activity'] = $this->now;\r\n\r\n\t\t$this->sess_write();\r\n\t}\r\n\r\n\t\/**\r\n\t * Override\r\n\t *\/\r\n\tpublic function sess_destroy() {\r\n\t\tif ($this->use_not_php_session()) {\r\n\t\t\tparent::sess_destroy();\r\n\t\t\treturn;\r\n\t\t}\r\n\t\t\r\n\t\tif (isset($_COOKIE[session_name()])) {\r\n\t\t\tsetcookie(session_name(), '', time()-42000, '\/');\r\n\t\t}\r\n\r\n\t\t$this->userdata = array();\r\n\t\t$this->sess_write();\r\n\t\t\r\n\t\tsession_destroy();\r\n\t}\r\n\r\n\t\/**\r\n\t * PHP\u6a19\u6e96Session\u3092\u5229\u7528\u3059\u308b\u304b\u3092\u8fd4\u3059\u3002\r\n\t * @return bool\r\n\t *\/\r\n\tprotected function use_php_session() {\r\n\t\treturn $this->sess_use_php_session === true;\r\n\t}\r\n\r\n\t\/**\r\n\t * PHP\u6a19\u6e96Session\u3092\u5229\u7528\u3057\u306a\u3044\u304b\u3092\u8fd4\u3059\u3002\r\n\t * @return bool\r\n\t *\/\r\n\tprotected function use_not_php_session() {\r\n\t\treturn !$this->use_php_session();\r\n\t}\r\n}\r\n?>\r\n<\/pre>\n

\u307e\u305f\u3001\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u306b\u4e0b\u8a18\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n

system\/application\/config\/config.php<\/h3>\n
\r\n\/\/ PHP\u6a19\u6e96\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u5229\u7528\u3059\u308b\u304b\r\n$config['sess_use_php_session'] = TRUE;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

CodeIgniter\u304c\u63d0\u4f9b\u3057\u3066\u3044\u308b\u30bb\u30c3\u30b7\u30e7\u30f3\u30af\u30e9\u30b9\u306f\u3001Cookie\u306b\u5168\u3066\u4fdd\u5b58\u3059\u308b\u304b\u3001\u30e6\u30fc\u30b6\u304c\u8ffd\u52a0\u3057\u305f\u30c7\u30fc\u30bf\u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u4fdd\u5b58\u3057\u3066\u304a\u3044\u3066\u3001\u30ad\u30fc\u306fCookie\u306b\u4fdd\u5b58\u3059\u308b\u304b\u306e2\u7a2e\u985e\u3092\u9078\u629e\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e … \u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":2,"featured_media":9223372036854775807,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1980],"tags":[153],"_links":{"self":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts\/4597"}],"collection":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/comments?post=4597"}],"version-history":[{"count":0,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts\/4597\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/media\/9223372036854775807"}],"wp:attachment":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/media?parent=4597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/categories?post=4597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/tags?post=4597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}