JavaScript 获取卡密剩余使用时间 - 前端代码示例

以下代码演示了如何使用 JavaScript 从服务器获取卡密剩余使用时间,并将其显示在页面上:

function getRemainingTime() {
  var card = getCookie('card'); // 获取 cookie 中的卡密
  var xhr = new XMLHttpRequest();
  xhr.open('GET', 'time.php?card=' + card, true);
  xhr.onreadystatechange = function() {
    if (xhr.readyState === XMLHttpRequest.DONE) {
      if (xhr.status === 200) {
        document.getElementById('remaining-time').textContent =
          '卡密剩余使用时间:' + xhr.responseText;
      } else {
        console.log('Error: ' + xhr.status);
      }
    }
  };
  xhr.send();
}

后端 PHP 文件:time.php

以下代码演示了如何使用 PHP 从数据库获取卡密剩余使用时间:

<?php
  include('con.php');
  $card = $_GET['card'];
  $query = mysqli_query($con, "SELECT expire FROM cardh WHERE card='$card'" );
  $result = mysqli_fetch_array($query);
  $expire = strtotime($result['expire']);
  $current_time = time();
  $remaining_time = $expire - $current_time;
  echo $remaining_time;
?>

说明:

  • getCookie('card') 用于获取存储在 cookie 中的卡密信息。
  • XMLHttpRequest 用于发送请求到 time.php 获取剩余时间数据。
  • time.php 通过 mysqli_query 查询数据库 cardh 表获取卡密剩余使用时间,并计算剩余时间。
  • document.getElementById('remaining-time').textContent 用于将获取到的剩余时间数据显示在页面元素上。

注意:

  • 请确保 con.php 文件已正确链接到数据库。
  • 请确保 cardh 表中包含 cardexpire 列,expire 列存储卡密过期时间。
  • 可以根据实际情况调整代码,例如修改页面元素 ID 或数据获取方式。

本示例提供了一种简单的解决方案,可以根据您的需求进行扩展和定制。

JavaScript 获取卡密剩余使用时间 - 前端代码示例

原文地址: https://www.cveoy.top/t/topic/munc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录