PHP - session_set_cookie_params() 函式



定義和用法

Session 或 session 處理是一種使資料可在 Web 應用程式的各個頁面中訪問的方法。session_set_cookie_params() 用於設定在 php.ini 檔案中定義的 session cookie 引數。

語法

session_set_cookie_params([$array]);

引數

序號 引數和描述
1

陣列(可選)

這是一個關聯陣列,它儲存 cookie 引數的值(生命週期、路徑、域、安全、httponly 和 samesite)。

返回值

此函式返回一個布林值,成功時為 TRUE,失敗時為 FALSE。

PHP 版本

此函式首次在 PHP 4 版本中引入,並在所有後續版本中有效。

示例 1

以下示例演示了 session_set_cookie_params() 函式的用法。

<html>   
   <head>
      <title>Setting up a PHP session</title>
   </head>   
   <body>
      <?php  	
         //Setting the cookie parameters
         session_set_cookie_params(30 * 60, "/", "test", );
         //Retrieving the cookie parameters
         $res = session_get_cookie_params();
         //Starting the session
         session_start();
         print_r($res);	  
      ?>
   </body>   
</html> 

執行上述 html 檔案後,它將顯示以下訊息:

Array ( [lifetime] => 1800 [path] => /test [domain] => test.com [secure] => [httponly] => [samesite] => )

示例 2

這是此函式的另一個示例。

<html>   
   <head>
      <title>Setting up a PHP session</title>
   </head>   
   <body>
      <?php  
         //Retrieving the cookie parameters
         $currentCookieParams = session_get_cookie_params();
         
         //Setting the cookie parameters
         $domain = '.test.com';
         session_set_cookie_params(
            $currentCookieParams["lifetime"],
            $currentCookieParams["path"],
            $domain,
            $currentCookieParams["secure"],
            $currentCookieParams["httponly"]
         );
         //Starting the session
         session_start();
      ?>
   </body>   
</html>
php_function_reference.htm
廣告