当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP session_cache_expire()用法及代码示例



定义和用法

会话或会话处理是一种使数据跨 Web 应用程序的各个页面可用的方法。这session_cache_expire()函数用于获取当前缓存过期时间。

用法

session_cache_expire([$new_cache_expire]);

参数

Sr.No 参数及说明
1

new_cache_expire (Optional)

这是一个整数值,表示新的缓存过期值。

返回值

此函数返回当前缓存过期值。

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

点击时Next执行以下文件。

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 - session_cache_expire() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。