mkach/AnonymousID.php
Lain Iwakura c5ad7e8cb4
ok?
2025-07-24 05:58:36 +03:00

50 lines
1.3 KiB
PHP

<?php
class AnonymousID {
private $db;
private $ip;
private $boardId;
public function __construct($db, $ip, $boardId) {
$this->db = $db;
$this->ip = $ip;
$this->boardId = $boardId;
}
public function getOrCreateID() {
$stmt = $this->db->prepare('
SELECT anonymous_id FROM posts
WHERE ip_address = ? AND board_id = ?
ORDER BY created_at DESC
LIMIT 1
');
$stmt->execute([$this->ip, $this->boardId]);
$existingId = $stmt->fetchColumn();
if ($existingId) {
return $existingId;
}
return $this->generateNewID();
}
private function generateNewID() {
return 'ID:' . sprintf('%06d', mt_rand(1, 999999));
}
public function getIDForThread() {
$stmt = $this->db->prepare('
SELECT anonymous_id FROM threads
WHERE ip_address = ? AND board_id = ?
ORDER BY created_at DESC
LIMIT 1
');
$stmt->execute([$this->ip, $this->boardId]);
$existingId = $stmt->fetchColumn();
if ($existingId) {
return $existingId;
}
return $this->generateNewID();
}
}