php 签到实现
PHP 签到系统实现方法
数据库设计
创建用户表和签到记录表,用于存储用户信息和签到数据。用户表通常包含用户ID、用户名等基本信息,签到记录表包含用户ID、签到日期等字段。
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL
);
CREATE TABLE check_in_records (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
check_in_date DATE NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id)
);
签到功能实现
检查用户是否已签到,未签到则插入签到记录。使用当前日期进行判断,避免重复签到。
function checkIn($userId) {
$today = date('Y-m-d');
$pdo = new PDO('mysql:host=localhost;dbname=your_db', 'username', 'password');
$stmt = $pdo->prepare("SELECT * FROM check_in_records WHERE user_id = ? AND check_in_date = ?");
$stmt->execute([$userId, $today]);
if ($stmt->fetch()) {
return '今日已签到';
} else {
$stmt = $pdo->prepare("INSERT INTO check_in_records (user_id, check_in_date) VALUES (?, ?)");
$stmt->execute([$userId, $today]);
return '签到成功';
}
}
连续签到统计
统计用户连续签到的天数,增强用户粘性。通过查询最近签到记录计算连续天数。
function getContinuousDays($userId) {
$pdo = new PDO('mysql:host=localhost;dbname=your_db', 'username', 'password');
$stmt = $pdo->prepare("SELECT check_in_date FROM check_in_records WHERE user_id = ? ORDER BY check_in_date DESC");
$stmt->execute([$userId]);
$continuousDays = 0;
$previousDate = null;
while ($row = $stmt->fetch()) {
$currentDate = new DateTime($row['check_in_date']);
if ($previousDate === null) {
$continuousDays = 1;
$previousDate = $currentDate;
continue;
}
$diff = $previousDate->diff($currentDate);
if ($diff->days === 1) {
$continuousDays++;
$previousDate = $currentDate;
} else {
break;
}
}
return $continuousDays;
}
奖励机制实现
根据连续签到天数给予不同奖励,激励用户持续签到。可结合用户积分系统实现。
function giveReward($userId, $continuousDays) {
$rewardPoints = 0;
if ($continuousDays >= 7) {
$rewardPoints = 50;
} elseif ($continuousDays >= 3) {
$rewardPoints = 20;
} else {
$rewardPoints = 10;
}
$pdo = new PDO('mysql:host=localhost;dbname=your_db', 'username', 'password');
$stmt = $pdo->prepare("UPDATE users SET points = points + ? WHERE id = ?");
$stmt->execute([$rewardPoints, $userId]);
return $rewardPoints;
}
安全防护措施
防止签到系统被滥用,增加必要的安全验证。包括用户身份验证和防刷机制。
function safeCheckIn($userId) {
session_start();
if (!isset($_SESSION['user_id']) || $_SESSION['user_id'] != $userId) {
return '未授权操作';
}
$ip = $_SERVER['REMOTE_ADDR'];
$today = date('Y-m-d');
$pdo = new PDO('mysql:host=localhost;dbname=your_db', 'username', 'password');
$stmt = $pdo->prepare("SELECT COUNT(*) FROM check_in_records WHERE ip = ? AND check_in_date = ?");
$stmt->execute([$ip, $today]);
if ($stmt->fetchColumn() > 3) {
return '签到过于频繁';
}
return checkIn($userId);
}
签到日历展示
展示用户本月签到情况,提供直观的视觉反馈。生成包含签到状态的日历视图。
function getMonthlyCheckIns($userId, $year, $month) {
$pdo = new PDO('mysql:host=localhost;dbname=your_db', 'username', 'password');
$startDate = "$year-$month-01";
$endDate = date('Y-m-t', strtotime($startDate));
$stmt = $pdo->prepare("SELECT check_in_date FROM check_in_records
WHERE user_id = ? AND check_in_date BETWEEN ? AND ?");
$stmt->execute([$userId, $startDate, $endDate]);
$checkInDates = [];
while ($row = $stmt->fetch()) {
$checkInDates[] = $row['check_in_date'];
}
return $checkInDates;
}
以上实现方法涵盖了签到系统的主要功能模块,可根据实际需求进行调整和扩展。数据库连接部分需替换为实际配置,并建议使用预处理语句防止SQL注入。







