詳解PHP會話儲存方式_PHP教程

來源:互聯網
上載者:User
  先確認會話是否自動開啟還是需要通過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。

http://www.bkjia.com/PHPjc/371875.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/371875.htmlTechArticle先確認會話是否自動開啟還是需要通過session_start()來手動開啟: ; 指定會話模組是否在請求開始時自動啟動一個會話。預設為 0(不啟動) ;...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.