简体   繁体   English

PHP会话锁

[英]php session lock

I want the code snippet 我想要代码片段

 echo "This is a test";

to be printed once every hour. 每小时打印一次。 So when the user loades index.php the first time it should be printed. 因此,当用户首次加载index.php时,应将其打印出来。 When the user immediately after that reloads the page it should dissapear. 当此后用户立即重新加载页面时,它应该消失。 After one hour it should be printed again... How can I do this? 一小时后,应该再次打印...我该怎么做? Thanks. 谢谢。

This should work: 这应该工作:

session_start();

if (!isset($_SESSION["last_shown"]))             // If the session variable 
                                                 // was never set
or ($_SESSION["last_shown"] < (time() - 3600))   // or was set more than
                                                 // 1 hour (3600 secs) ago
 {
  echo "This is a test";                         // Make output
  $_SESSION["last_shown"] = time();              // Update session variable 
                                                 // with current time
 }

Rather than sessions, set a cookie to expire in 1 hour. 将会话设置为在1小时后过期,而不是会话。 on page load, if the cookie is there don't display the message. 在页面加载时,如果其中存在cookie,则不会显示该消息。 The advantage over sessions is that the user can close the browser and return later (if you want that) 与会话相比,优点是用户可以关闭浏览器并稍后返回(如果需要)

if (!isset($_COOKIE['sesslock']))
{
  // No cookie - show message & set cookie (expires in 1 hour, 3600sec)
  setcookie('sesslock','ok', time()+3600);
  echo "this is a test";
}
else
{
  // The cookie is there, don't display your message
}

you can set the current time to a $_SESSION variable and if the user changes the page check the session time variable. 您可以将当前时间设置为$ _SESSION变量,如果用户更改页面,请检查会话时间变量。 and if that time is greater than one hour, than display the message 如果该时间大于一小时,则显示该消息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM