File: //opt/proundcontra.mosmuc.de/signup.php
<?php
/***************************************************
* signup.php
*
* Full-page example for user signup with your DB.
* Adjust DB connection details as needed!
***************************************************/
session_start();
// 1) SET YOUR DB CONNECTION HERE:
$dbHost = 'localhost';
$dbUser = 'mosmuc_pc';
$dbPassword = 'mmpc&44823';
$dbName = 'mosmuc_pc';
try {
$pdo = new PDO("mysql:host=$dbHost;dbname=$dbName;charset=utf8", $dbUser, $dbPassword, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
} catch (Exception $ex) {
die("DB connection error: " . $ex->getMessage());
}
// If user is already logged in, we might want to redirect.
// If you prefer to let a logged-in user see signup again, remove these lines:
if (!empty($_SESSION['logged_in'])) {
echo "SIGNUP_ERROR_ALREADY_LOGGED_IN";
exit;
}
// We'll store errors or success in these:
$errorMsg = '';
$successMsg = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username'] ?? '');
$email = trim($_POST['email'] ?? '');
$password1 = trim($_POST['password'] ?? '');
$password2 = trim($_POST['password2'] ?? '');
// Basic validation
if ($username === '' || $email === '' || $password1 === '' || $password2 === '') {
$errorMsg = "All fields are required.";
} elseif ($password1 !== $password2) {
$errorMsg = "SIGNUP_ERROR_PASSWORD_MISMATCH (passwords don’t match).";
} else {
// Check if user or email exist:
$stmt = $pdo->prepare("SELECT userId FROM users WHERE userName = :uname OR email = :email LIMIT 1");
$stmt->execute([':uname' => $username, ':email' => $email]);
$existing = $stmt->fetch(PDO::FETCH_ASSOC);
if ($existing) {
// Distinguish if it's the username or email:
// For simplicity, we do not differentiate in code, but you can:
$errorMsg = "SIGNUP_ERROR_USERNAME_EXISTS or SIGNUP_ERROR_EMAIL_IN_USE";
} else {
// Generate a random salt:
$salt = bin2hex(random_bytes(16));
// Create a combined password to hash:
$passwordWithSalt = $password1 . $salt;
// Hash it:
$hash = password_hash($passwordWithSalt, PASSWORD_DEFAULT);
// Insert new user:
$now = time(); // big int
try {
$insert = $pdo->prepare("
INSERT INTO users
(userName, email, `group`, password, salt, dateAdded, user_last_action, scoreQuestions, scoreArguments)
VALUES
(:uname, :email, :ugroup, :pwd, :salt, :dateAdded, :lastAction, 0, 0)
");
$insert->execute([
':uname' => $username,
':email' => $email,
':ugroup' => 1, // 1 = normal user, or 2 = admin, etc. Adjust as needed.
':pwd' => $hash,
':salt' => $salt,
':dateAdded' => $now,
':lastAction'=> $now
]);
// If you want auto-login after signup, you can do:
// $_SESSION['logged_in'] = true;
// $_SESSION['user_id'] = $pdo->lastInsertId();
// $_SESSION['user_name'] = $username;
// $_SESSION['user_role'] = 1; // or 2 for admin
$successMsg = "SIGNUP_SUCCESS: Check your email for confirmation, or proceed to login.";
} catch (Exception $ex) {
$errorMsg = "SIGNUP_ERROR_GENERAL: " . $ex->getMessage();
}
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Sign Up - BPTArguments</title>
</head>
<body>
<h1>Sign Up</h1>
<?php if ($errorMsg): ?>
<div style="color:red;"><?php echo htmlspecialchars($errorMsg); ?></div>
<?php elseif ($successMsg): ?>
<div style="color:green;"><?php echo htmlspecialchars($successMsg); ?></div>
<p><a href="/login">Go to Login</a></p>
<?php exit; ?>
<?php endif; ?>
<form action="/signup" method="POST">
<p>
<label>Username:<br/>
<input type="text" name="username" value="<?php echo htmlspecialchars($username ?? ''); ?>" required>
</label>
</p>
<p>
<label>Email:<br/>
<input type="email" name="email" value="<?php echo htmlspecialchars($email ?? ''); ?>" required>
</label>
</p>
<p>
<label>Password:<br/>
<input type="password" name="password" required>
</label>
</p>
<p>
<label>Repeat Password:<br/>
<input type="password" name="password2" required>
</label>
</p>
<p>
<button type="submit">SIGNUP_SUBMIT</button>
</p>
</form>
<p>Already have an account? <a href="/login">Login Here</a></p>
</body>
</html>