首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP网站的动态时间

问PHP网站的动态时间
EN

Stack Overflow用户
提问于 2015-07-12 01:31:04
回答 1查看 129关注 0票数 2

我正在为我的团队制作一个网站,其中有几个游戏服务器。在这个过程中,我制作了一个网站,点击服务器,作为回报,显示它是向上还是向下。我想说,如果它是坏的,你可以发邮件给我。那部分起作用。我不想让一个用户在他们发了一次邮件后继续给我发邮件。

我想知道我是否可以做一个脚本,当任何用户点击链接给我发电子邮件时,没有其他用户可以给我发一个小时的电子邮件。我想这必须是服务器端的东西。我在过去做了一个脚本,当有人点击链接时,它可以工作一个小时。问题是,当用户返回到该目录时,他们可以再次单击它,因为时间没有节省。我还希望,如果多个用户同时单击该链接,它只会增加1个小时,而不是多个小时(例如,3个用户在网站上单击“通知”,就会增加2个小时,而不是仅仅增加1个小时。)

任何正确方向的暗示都会很好。我考虑过使用MySQL,但是除非绝对需要,否则我不想使用(不知道我们的数据库设置有多可能)。

EN

回答 1

Stack Overflow用户

发布于 2015-07-12 02:06:53

另一种选择是让一个文件位于服务器的某个位置,其中包含一个文件,其中包含在其中写入最后一条消息的时间,然后将其与当前时间进行比较。下面是一个粗略的示例(请注意,该示例不安全,需要在接受原始用户输入之前进行净化,但希望它将为您指明正确的方向):

代码语言:javascript
复制
<?php
send_email();

function maindir() {
  // This will need to be set to the directory containing your time file.
  $cwd = '/home/myusername/websites/example.com';
  return $cwd;
}

function update_timefile() {
  $cwd = maindir();
  // The file that will contain the time.
  $timefile = 'timefile.txt';
  $time = time();
  file_put_contents("$cwd/$timefile", $time);
}

function send_email() {
  // Note: this should be sanitized more and have security checks performed on it.
  // It also assumes that your user's subject and message have been POSTed to this
  // .php file.
  $subject = ($_POST && isset($_POST['subject']) && !empty($_POST['subject'])) ? $_POST['subject'] ? FALSE;
  $message = ($_POST && isset($_POST['message']) && !empty($_POST['message'])) ? $_POST['message'] ? FALSE;
  if ($subject && $message) {
    $to = 'me@example.com';
    $cwd = maindir();
    $timefile = 'timefile.txt';
    // Current time
    $timenow = time();
    // Read the time from the time file
    $timeget = file_get_contents("$cwd/$timefile");
    // Calculate the difference
    $timediff = $timenow - $timeget;
    // If the difference is greater than or equal to the current time + 3600 seconds..
    if ($timediff >= 3600) {
      // ... and if the message gets sent...
      if (mail($to, $subject, $message)) {
        // ... update the time file.
        update_timefile();
      }
    }
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31363498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档