先写一个链接数据库的php页面conphp再写一个php页面carphp往数据表cars中插入card expire days 3个字段写个按钮可以手动输入卡密数据多少天days其他2项随机生成最后写一个简易html页面这个html页面实现通过输入卡密才能访问
con.php:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接是否成功
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
?>
car.php:
<?php
include 'con.php'; // 链接数据库
if(isset($_POST['submit'])) { // 判断是否点击了提交按钮
$num = $_POST['num']; // 获取输入的卡密数量
$expire = date("Y-m-d H:i:s", strtotime("+1 year")); // 生成过期时间,假设为一年后
$cards = array(); // 存储卡密数据的数组
for($i = 0; $i < $num; $i++) { // 循环插入卡密数据
$card = generateCard(); // 生成卡密
$days = $_POST['days']; // 获取输入的天数
$sql = "INSERT INTO cars (card, expire, days) VALUES ('$card', '$expire', '$days')"; // SQL语句
if ($conn->query($sql) === TRUE) { // 执行SQL语句
$cards[] = $card; // 存储卡密数据
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close(); // 关闭数据库连接
}
function generateCard() { // 生成卡密
$card = '';
$chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for($i = 0; $i < 16; $i++) {
$card .= $chars[rand(0, strlen($chars)-1)];
}
return $card;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>生成卡密</title>
</head>
<body>
<form method="post">
<label for="num">卡密数量:</label>
<input type="number" name="num" id="num" required><br>
<label for="days">天数:</label>
<input type="number" name="days" id="days" required><br>
<input type="submit" name="submit" value="生成">
</form>
<?php if(isset($cards)) { // 如果卡密数据已经生成 ?>
<h3>卡密数据:</h3>
<ul>
<?php foreach($cards as $card) { ?>
<li><?php echo $card; ?></li>
<?php } ?>
</ul>
<?php } ?>
</body>
</html>
index.html:
<!DOCTYPE html>
<html>
<head>
<title>输入卡密才能访问</title>
</head>
<body>
<form method="post" action="check.php">
<label for="card">请输入卡密:</label>
<input type="text" name="card" id="card" required><br>
<input type="submit" name="submit" value="提交">
</form>
</body>
</html>
check.php:
<?php
include 'con.php'; // 链接数据库
if(isset($_POST['submit'])) { // 判断是否点击了提交按钮
$card = $_POST['card']; // 获取输入的卡密
$sql = "SELECT * FROM cars WHERE card='$card'"; // SQL语句
$result = $conn->query($sql);
if ($result->num_rows > 0) { // 如果查询到结果
$row = $result->fetch_assoc();
$expire = strtotime($row['expire']);
$now = strtotime(date("Y-m-d H:i:s"));
if($expire > $now) { // 如果卡密未过期
$days = $row['days'];
echo "恭喜,卡密有效,有效期为 $days 天。";
} else { // 如果卡密已过期
echo "卡密无效,已过期。";
}
} else { // 如果未查询到结果
echo "卡密无效。";
}
$conn->close(); // 关闭数据库连接
}
?>
原文地址: http://www.cveoy.top/t/topic/LGp 著作权归作者所有。请勿转载和采集!