-
Notifications
You must be signed in to change notification settings - Fork 0
/
signup.php
37 lines (33 loc) · 1.24 KB
/
signup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
session_start();
require 'db.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Collecting data from form
$username = trim($_POST['username']);
$email = trim($_POST['email']);
$password = $_POST['password'];
// Validation
if (empty($username) || empty($email) || empty($password)) {
echo "All fields are required!";
exit;
}
// Check if user exists
$stmt = $pdo->prepare("SELECT * FROM users WHERE email = ? OR username = ?");
$stmt->execute([$email, $username]);
if ($stmt->rowCount() > 0) {
echo "User already exists. Please use a different email or username.";
exit();
}
// Hash password and insert user into database
$hashedPassword = password_hash($password, PASSWORD_BCRYPT);
$stmt = $pdo->prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)");
if ($stmt->execute([$username, $email, $hashedPassword])) {
// Set a session message for success
$_SESSION['signup_success'] = "Signup successful! You can now login.";
header('Location: login_signup.php'); // Redirect to login page
exit();
} else {
echo "Something went wrong. Please try again.";
}
}
?>