View file zip0.ru/register.php

File size: 4.76Kb
<?php
require_once 'db.php';

$errors = [];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email    = trim($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';
    $name     = trim($_POST['name'] ?? '');
    $gender   = $_POST['gender'] ?? 'other';

    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors[] = 'Введите корректный email';
    }
    if (mb_strlen($password) < 6) {
        $errors[] = 'Пароль должен быть не короче 6 символов';
    }
    if ($name === '') {
        $errors[] = 'Введите имя';
    }

    if (!$errors) {
        $stmt = $mysqli->prepare("SELECT id FROM users WHERE email = ?");
        $stmt->bind_param('s', $email);
        $stmt->execute();
        $stmt->store_result();
        if ($stmt->num_rows > 0) {
            $errors[] = 'Этот email уже зарегистрирован';
        }
        $stmt->close();
    }

    if (!$errors) {
        $password_hash = password_hash($password, PASSWORD_DEFAULT);

        $stmt = $mysqli->prepare("
            INSERT INTO users (email, password_hash, name, gender, created_at)
            VALUES (?, ?, ?, ?, NOW())
        ");
        $stmt->bind_param('ssss', $email, $password_hash, $name, $gender);
        if ($stmt->execute()) {
            $new_id = $stmt->insert_id;
            $stmt->close();

            $_SESSION['user_id'] = $new_id;
            header('Location: profile.php');
            exit;
        } else {
            $errors[] = 'Ошибка при регистрации. Попробуйте позже.';
            $stmt->close();
        }
    }
}

include 'header.php';
?>

<style>
.auth-page {
    max-width:420px;
    margin:20px auto 40px;
}
.auth-title {
    font-size:26px;
    font-weight:700;
    margin-bottom:6px;
}
.auth-subtitle {
    font-size:14px;
    color:#7b7287;
    margin-bottom:18px;
}
.auth-card {
    background:#ffffff;
    border-radius:22px;
    padding:18px 18px 20px;
    box-shadow:0 14px 40px rgba(0,0,0,0.08);
}
.auth-field {
    margin-bottom:14px;
}
.auth-field label {
    display:block;
    font-size:13px;
    margin-bottom:4px;
    color:#6a5d76;
}
.auth-field input,
.auth-field select {
    width:100%;
    box-sizing:border-box;
}
.auth-error {
    background:#ffe5eb;
    color:#b3224a;
    border-radius:14px;
    padding:10px 12px;
    font-size:13px;
    margin-bottom:12px;
}
.auth-footer-text {
    margin-top:12px;
    font-size:13px;
    color:#7b7287;
    text-align:center;
}
.auth-footer-text a {
    color:#ff4f8b;
    text-decoration:none;
}
.auth-footer-text a:hover {
    text-decoration:underline;
}
</style>

<div class="auth-page">
    <div class="auth-title">Присоединяйся к SoftLove 💕</div>
    <div class="auth-subtitle">Создайте аккаунт, чтобы знакомиться с людьми и находить уютные совпадения.</div>

    <div class="auth-card">
        <?php if ($errors): ?>
            <div class="auth-error">
                <?php foreach ($errors as $e): ?>
                    <div><?= htmlspecialchars($e) ?></div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>

        <form method="post">
            <div class="auth-field">
                <label for="email">Email</label>
                <input id="email" type="email" name="email" required
                       value="<?= isset($email) ? htmlspecialchars($email) : '' ?>">
            </div>

            <div class="auth-field">
                <label for="password">Пароль</label>
                <input id="password" type="password" name="password" required>
            </div>

            <div class="auth-field">
                <label for="name">Имя</label>
                <input id="name" type="text" name="name" required
                       value="<?= isset($name) ? htmlspecialchars($name) : '' ?>">
            </div>

            <div class="auth-field">
                <label for="gender">Я</label>
                <select id="gender" name="gender">
                    <option value="m" <?= (isset($gender) && $gender === 'm') ? 'selected' : '' ?>>Мужчина</option>
                    <option value="f" <?= (isset($gender) && $gender === 'f') ? 'selected' : '' ?>>Женщина</option>
                    <option value="other" <?= (isset($gender) && $gender === 'other') ? 'selected' : '' ?>>Другое</option>
                </select>
            </div>

            <button type="submit" class="btn-primary" style="width:100%;margin-top:4px;">Создать аккаунт</button>
        </form>

        <div class="auth-footer-text">
            Уже зарегистрированы? <a href="login.php">Войти</a>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>