php签到程序实现

PHP签到程序实现
数据库设计
创建数据库表用于存储用户签到记录,表结构如下:
CREATE TABLE `sign_records` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`sign_date` date NOT NULL,
`sign_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`continuous_days` int(11) DEFAULT 1,
PRIMARY KEY (`id`),
UNIQUE KEY `user_date` (`user_id`,`sign_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
签到核心功能
<?php
// 连接数据库
$db = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
function signIn($userId) {
global $db;
$today = date('Y-m-d');
$yesterday = date('Y-m-d', strtotime('-1 day'));
// 检查今天是否已签到
$stmt = $db->prepare("SELECT COUNT(*) FROM sign_records WHERE user_id = ? AND sign_date = ?");
$stmt->execute([$userId, $today]);
if ($stmt->fetchColumn() > 0) {
return ['status' => 0, 'msg' => '今日已签到'];
}
// 检查昨天是否签到
$stmt = $db->prepare("SELECT continuous_days FROM sign_records WHERE user_id = ? AND sign_date = ?");
$stmt->execute([$userId, $yesterday]);
$continuousDays = $stmt->fetchColumn();
$currentContinuous = $continuousDays ? $continuousDays + 1 : 1;
// 插入签到记录
$stmt = $db->prepare("INSERT INTO sign_records (user_id, sign_date, continuous_days) VALUES (?, ?, ?)");
$result = $stmt->execute([$userId, $today, $currentContinuous]);
if ($result) {
return ['status' => 1, 'msg' => '签到成功', 'continuous_days' => $currentContinuous];
} else {
return ['status' => 0, 'msg' => '签到失败'];
}
}
?>
签到记录查询
function getSignRecords($userId, $month = null) {
global $db;
$month = $month ?: date('Y-m');
$startDate = $month . '-01';
$endDate = date('Y-m-t', strtotime($startDate));
$stmt = $db->prepare("SELECT sign_date FROM sign_records
WHERE user_id = ? AND sign_date BETWEEN ? AND ?");
$stmt->execute([$userId, $startDate, $endDate]);
$records = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$records[] = $row['sign_date'];
}
return $records;
}
前端调用示例
<div class="sign-container">
<button id="signBtn" onclick="doSign()">每日签到</button>
<div id="signResult"></div>
</div>
<script>
function doSign() {
fetch('sign.php?action=sign&user_id=123')
.then(response => response.json())
.then(data => {
if(data.status) {
document.getElementById('signResult').innerHTML =
`签到成功!已连续签到${data.continuous_days}天`;
} else {
document.getElementById('signResult').innerHTML = data.msg;
}
});
}
</script>
连续签到奖励机制
function checkSignReward($continuousDays) {
$rewards = [
3 => '10积分',
7 => '30积分',
15 => '50积分',
30 => '100积分'
];
if (isset($rewards[$continuousDays])) {
// 发放奖励逻辑
return $rewards[$continuousDays];
}
return null;
}
安全注意事项
- 用户ID应该从登录会话中获取,避免前端直接传递
- 添加CSRF防护措施
- 对数据库操作进行异常处理
- 重要操作建议记录日志
性能优化建议
- 对频繁查询的数据可以添加缓存
- 批量查询时使用索引优化
- 定期归档历史签到记录







