Files
PHP/register.php
2026-11-27 15:41:43 +08:00

105 lines
4.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
require_once __DIR__ . '/includes/auth_layout.php';
if (current_user($conn)) {
header('Location: gallery.php');
exit;
}
$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username'] ?? '');
$password = $_POST['password'] ?? '';
$nickname = trim($_POST['nickname'] ?? '');
$invitation_code = trim($_POST['invitation_code'] ?? '');
if (!preg_match('/^[A-Za-z0-9]{3,20}$/', $username)) {
$errors[] = '用户名需为 3–20 位英文或数字。';
}
if (!preg_match('/^[A-Za-z0-9!@_]{6,64}$/', $password)) {
$errors[] = '密码至少 6 位,只能包含英文字母、数字和 ! @ _。';
}
if (!preg_match('/^[\x{4e00}-\x{9fa5}A-Za-z0-9]{1,20}$/u', $nickname)) {
$errors[] = '昵称 1–20 位,只能包含中文、英文和数字。';
}
if (!$errors) {
// 邀请码必须未过期且剩余次数 > 0
$stmt = $conn->prepare("SELECT id, counts FROM invitation_codes WHERE code = ? AND expires_at > NOW() AND counts > 0 FOR UPDATE");
$conn->begin_transaction();
$stmt->bind_param("s", $invitation_code);
$stmt->execute();
$codeRow = $stmt->get_result()->fetch_assoc();
$stmt->close();
if (!$codeRow) {
$conn->rollback();
$errors[] = '邀请码无效、已过期或次数已用完。';
} else {
$check = $conn->prepare("SELECT user_id FROM photo_users WHERE username = ?");
$check->bind_param("s", $username);
$check->execute();
$exists = $check->get_result()->num_rows > 0;
$check->close();
if ($exists) {
$conn->rollback();
$errors[] = '该用户名已被占用。';
} else {
$hash = password_hash($password, PASSWORD_DEFAULT);
$ins = $conn->prepare("INSERT INTO photo_users (username, password, points, nick_name) VALUES (?, ?, ?, ?)");
$ins->bind_param("ssis", $username, $hash, $user_points, $nickname);
$ins->execute();
$ins->close();
$upd = $conn->prepare("UPDATE invitation_codes SET counts = counts - 1 WHERE id = ? AND counts > 0");
$upd->bind_param("i", $codeRow['id']);
$upd->execute();
$upd->close();
$conn->commit();
flash_set('success', "注册成功,已获赠 {$user_points} 次上传额度,请登录。");
header('Location: index.php');
exit;
}
}
}
}
render_auth_header('注册', '', 'square.php');
?>
<h2 class="auth__title">创建账号</h2>
<p class="auth__sub">需要管理员发放的邀请码 · 注册即获 <?php echo (int)$user_points; ?> 次上传额度</p>
<?php foreach ($errors as $e): ?>
<div class="alert alert--danger"><?php echo icon('warn'); ?><span><?php echo h($e); ?></span></div>
<?php endforeach; ?>
<form method="post">
<div class="field">
<label class="label" for="invitation_code">邀请码 <span class="req">*</span></label>
<input class="input" type="text" id="invitation_code" name="invitation_code"
value="<?php echo h($_POST['invitation_code'] ?? ''); ?>" placeholder="16 位邀请码" required>
</div>
<div class="field">
<label class="label" for="username">用户名</label>
<input class="input" type="text" id="username" name="username"
value="<?php echo h($_POST['username'] ?? ''); ?>" pattern="[A-Za-z0-9]{3,20}"
placeholder="3–20 位英文或数字" title="3–20 位英文或数字" required>
</div>
<div class="field">
<label class="label" for="nickname">昵称</label>
<input class="input" type="text" id="nickname" name="nickname"
value="<?php echo h($_POST['nickname'] ?? ''); ?>" maxlength="20" placeholder="图库里显示的名字" required>
</div>
<div class="field">
<label class="label" for="password">密码</label>
<div class="input-group">
<input class="input" type="password" id="password" name="password" minlength="6" placeholder="至少 6 位" required>
<button type="button" class="btn btn--ghost btn--icon" data-toggle-password="password" aria-label="显示密码"><?php echo icon('eye'); ?></button>
</div>
<div class="help">可用英文字母、数字和 ! @ _</div>
</div>
<button type="submit" class="btn btn--primary btn--lg btn--block">注册</button>
</form>
<p class="auth__foot">已有账号?<a href="index.php">去登录</a></p>
<?php render_auth_footer();