PHP网站的动态时间

时间:2015-07-12 01:31:05

标签: php web

我正在为我的小组制作一个托管几个游戏服务器的网站。在这个过程中,我创建了一个ping服务器的网站,并反过来显示它是上升还是下降。我想能够说,如果它失败了,你可以给我发电子邮件。那部分有效。我不想要的是用户能够在发送一次后给我发电子邮件。

我想知道我是否可以制作一个脚本,当任何用户点击链接给我发电子邮件时,其他用户可以给我发电子邮件大约一个小时。我认为这必须是服务器方面的东西。我过去制作了一个脚本,当有人点击链接时,它会增加一个小时。问题是当所述用户返回该目录时,他们可以再次点击它,因为时间没有保存。我也想要它,如果多个用户同时点击链接它只增加1小时,而不是多个(例如,3个用户在网站2用户单击通知它将添加2小时而不是1)

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

1 个答案:

答案 0 :(得分:1)

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

<?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();
      }
    }
  }
}