PHP - session_cache_limiter() 函式



定義和用法

會話或會話處理是一種使資料在 Web 應用程式的各個頁面之間可用的方法。HTTP 標頭決定客戶端如何快取頁面內容。您可以定義要傳送到客戶端的快取控制 HTTP 標頭,使用快取限制器。

session_cache_limiter() 函式用於獲取或設定當前會話的快取限制器。

語法

session_cache_limiter([$cache_limiter]);

引數

序號 引數及描述
1

cache_limiter (可選)

這是一個字串值,表示快取限制器的型別,可以是以下之一:

  • public
  • private_no_expire
  • private
  • nocache

返回值

此函式返回一個整數,表示建立的會話 ID。

PHP 版本

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

示例 1

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

<html>   
   <head>
      <title>Setting up a PHP session</title>
   </head>   
   <body>
      <?php  	
         //Retrieving the cache limiter
         $limiter = session_cache_limiter();
         print("Cache limiter: ".$limiter);
      ?>
   </body>   
</html>   

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

Cache limiter: nocache

示例 2

以下是此函式的另一個示例,在此示例中,我們在同一個會話中擁有來自同一應用程式的兩個頁面。

session_page1.htm

<?php
   if(isset($_POST['SubmitButton'])){ 
      //Setting the cache limiter
      session_cache_limiter('public');
      session_start();
      $_SESSION['name'] = $_POST['name'];
      $_SESSION['age']  = $_POST['age']; 
   }
?>
<html>
   <body>
      <form action="#" method="post">
         <br>
         <label for="fname">Enter the values click Submit and click on Next</label>
         <br><br><label for="fname">Name:</label>
         <input type="text" id="name" name="name"><br><br>
         <label for="lname">Age:</label>
         <input type="text" id="age" name="age"><br><br>           
         <input type="submit" name="SubmitButton"/>
         <?php echo '<br><br /><a href="session_page2.htm">Next</a>'; ?>
      </form>
   </body>
</html>

這將產生以下輸出:

Session Start

單擊 **下一個** 後,將執行以下檔案。

session_page2.htm

<html>   
   <head>
      <title>Second Page</title>
   </head>
   <body>
      <?php
         //Session started
         session_start();	
         //Retrieving the cache limiter
         $limiter = session_cache_limiter();
         print("Cache limiter: ".$limiter);
         echo "<br>";	  
         print_r($_SESSION);
      ?>   
   </body>   
</html>

這將產生以下輸出:

Cache limiter: nocache
Array ( [name] => krishna [age] => 30 )

示例 3

您可以使用此函式設定快取限制器,如下所示:

<html>   
   <head>
      <title>Setting up a PHP session</title>
   </head>   
   <body>
      <?php  	
         //Setting the cache limiter
         session_cache_limiter('public');
         //Retrieving the cache limiter
         $limiter = session_cache_limiter();
         print("Cache limiter: ".$limiter);
      ?>
   </body>   
</html>

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

Cache limiter: public 
php_function_reference.htm
廣告