PHP - session_cache_expire() 函式



定義和用法

Session 或 session 處理是一種使資料在 Web 應用程式的各個頁面之間可用的方法。session_cache_expire() 函式用於獲取當前快取過期時間。

語法

session_cache_expire([$new_cache_expire]);

引數

序號 引數 & 說明
1

new_cache_expire (可選)

這是一個整數,表示新的快取過期值。

返回值

此函式返回當前快取過期值。

PHP 版本

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

示例 1

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

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

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

Cache limiter: 180

訊息中的數字會根據您在不關閉瀏覽器的情況下重新整理頁面的次數而變化。例如,如果您重新整理 10 次,則同一頁面將顯示以下訊息:

You have visited this page 16 times in this session.

示例 2

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

session_page1.htm

<?php
   if(isset($_POST['SubmitButton'])){
      //Setting the cache limiter
      session_cache_limiter('public');
      //Setting the session expire
      session_cache_expire(30);
      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 expire
         print("Cache Expire: ".session_cache_expire());
         echo "<br>";	  
         print_r($_SESSION);
      ?>   
   </body>   
</html>

這將產生以下輸出:

Cache Expire: 180
Array ( [name] => krishna [age] => 30 ) 

示例 3

您可以使用此函式設定會話過期時間,如下所示:

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

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

Cache limiter: 30
php_function_reference.htm
廣告