先確認會話是否自動開啟還是需要通過session_start()來手動開啟:
; 指定會話模塊是否在請求開始時自動啟動一個會話。默認為 0(不啟動)
; Initialize session on request startup.
; http://php.net/session.auto-start
session.auto_start = 0
在客戶端,會話可以存儲在cookie或者通過URL參數來獲取。依賴於服務器的配置:
; 指定是否在客戶端用 cookie 來存放會話 ID。默認為 1(啟用)
; Whether to use cookies.
; http://php.net/session.use-cookies
session.use_cookies = 1
; 指定是否在客戶端僅僅使用 cookie 來存放會話 ID。。啟用此設定可以防止有關通過 URL 傳遞會話 ID 的攻擊。
; This option forces PHP to fetch and use a cookie for storing and maintaining
; the session id. We encourage this operation as it's very helpful in combatting
; session hijacking when not specifying and managing your own session id. It is
; not the end all be all of session hijacking defense, but it's a good start.
; http://php.net/session.use-only-cookies
session.use_only_cookies = 1
如果確認存儲在cookie中,則可以進一點配置會話存儲在cookie中的各項配置,如cookie_name,cookie_lifetime,cookie_path,cookie_domain,cookie_secure,cookie_httponly
; Name of the session (used as cookie name).
; http://php.net/session.name
session.name = PHPSESSID
; Lifetime in seconds of cookie or, if 0, until browser is restarted.
; http://php.net/session.cookie-lifetime
session.cookie_lifetime = 0
; The path for which the cookie is valid.
; http://php.net/session.cookie-path
session.cookie_path = /
; The domain for which the cookie is valid.
; http://php.net/session.cookie-domain
session.cookie_domain =
; Whether or not to add the httpOnly flag to the cookie, which makes it inaccessible to browser scripting languages such as JavaScript.
; http://php.net/session.cookie-httponly
session.cookie_httponly =
在服務器端,同樣也可以通過多種方式來存儲會話。默認會話存儲在文件中,此時session.save_path為創建存儲文件的路徑。
; Handler used to store/retrieve data.
; http://php.net/session.save-handler
session.save_handler = files
; Argument passed to save_handler. In the case of files, this is the path
; where data files are stored. Note: Windows users have to change this
; variable in order to use PHP
's session functions.
;
; The path can be defined as:
;
; session.save_path = "N;/path"
;
; where N is an integer. Instead of storing all the session files in
; /path, what this will do is use subdirectories N-levels deep, and
; store the session data in those directories. This is useful if you
; or your OS have problems with lots of files in one directory, and is
; a more efficient layout for servers that handle lots of sessions.
;
; NOTE 1: PHP will not create this directory structure automatically.
; You can use the script in the ext/session dir for that purpose.
; NOTE 2: See the section on garbage collection below if you choose to
; use subdirectories for session storage
;
; The file storage module creates files using mode 600 by default.
; You can change that by using
;
; session.save_path = "N;MODE;/path"
;
; where MODE is the octal representation of the mode. Note that this
; does not overwrite the process's umask.
; http://php.net/session.save-path
;session.save_path = "/tmp"
PHP支持通過session_set_save_handler來實現會話處理器的自定義open, close, read, write, destroy, gc處理函數,常見的會話處理器包括使用內存型分配(如mm,memcache等),也可以使用數據庫進行存儲。由此可見,若需要會話存儲與文件系統(例如用數據庫PostgreSQL Session Save Handler或默認的文件存儲files)協同工作的,此時有可能造成用戶定制的會話處理器丟失了未存儲數據的會話。若使用內存型分配存儲,又需要考慮會話持久化存儲問題。
接下來重點講解memcache(d?)會話處理器。
Memcache模塊提供了於memcached方便的面向過程及面向對象的接口,memcached是為了降低動態web應用 從數據庫加載數據而產生的一種常駐進程緩存產品。
Memcache模塊同時提供了一個session 處理器 (memcache).
更多關於memcached的信息請參見» http://www.memcached.org/.
memcached是一個高性能分布式的內存對象緩存系統, 通常被用於降低數據庫加載壓力以提高動態web應用的響應速度。
此擴展使用了libmemcached庫提供的api與memcached服務端進行交互。它同樣提供了一個session處理器(memcached)。 它同時提供了一個session處理器(memcached)。
關於libmemcached的更多信息可以在» http://libmemcached.org/libMemcached.html查看。
memcache會話處理器配置:
session.save_handler = memcache
session.save_path = "tcp://127.0.0.1:11211?persistent=0&weight=1&timeout=1&retry_interval=15,tcp://127.0.0.1:11212?persistent=0&weight=1&timeout=1&retry_interval=15,tcp://127.0.0.1:11213?persistent=0&weight=1&timeout=1&retry_interval=15,tcp://127.0.0.1:11214?persistent=0&weight=1&timeout=1&retry_interval=15"
數據庫處理器可以使用Session PgSQL來實現(此擴展被認為已無人維護)。也可以使用其它數據庫來實現會話存儲,只不過需要自定義處理器函數function.session-set-save-handler.php。具體自定義處理器可參見maria at junkies dot jp。