129 lines
5.5 KiB
PHP
129 lines
5.5 KiB
PHP
<?php
|
|
// Include util functions
|
|
require_once("./assets/php/utils.php");
|
|
|
|
// Check if the user is already logged in, if yes then redirect him to welcome page
|
|
if (is_connected()) {
|
|
header("location: welcome.php");
|
|
exit;
|
|
}
|
|
|
|
// Define variables and initialize with empty values
|
|
$username = $password = $confirm_password = $first_name = $last_name = "";
|
|
$username_err = $password_err = $confirm_password_err = $first_name_err = $last_name_err = "";
|
|
|
|
// Processing form data when form is submitted
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
|
|
// Validate username
|
|
if (empty(trim($_POST["username"]))) {
|
|
$username_err = "Please enter an email address.";
|
|
} else {
|
|
if (get_username_count(trim($_POST["username"])) >= 1) {
|
|
$username_err = "This email is already taken.";
|
|
} else {
|
|
$username = trim($_POST["username"]);
|
|
}
|
|
}
|
|
|
|
// Validate password
|
|
if (empty(trim($_POST["password"]))) {
|
|
$password_err = "Please enter a password.";
|
|
} elseif (strlen(trim($_POST["password"])) < $MINIMAL_PASSWORD_LENGTH) {
|
|
$password_err = "Password must have atleast $MINIMAL_PASSWORD_LENGTH characters.";
|
|
} else {
|
|
$password = trim($_POST["password"]);
|
|
}
|
|
|
|
// Validate confirm password
|
|
if (empty(trim($_POST["confirm_password"]))) {
|
|
$confirm_password_err = "Please confirm password.";
|
|
} else {
|
|
$confirm_password = trim($_POST["confirm_password"]);
|
|
if (empty($password_err) && ($password != $confirm_password)) {
|
|
$confirm_password_err = "Password did not match.";
|
|
}
|
|
}
|
|
|
|
if (empty(trim($_POST["first_name"]))) {
|
|
$first_name_err = "Please input your first name.";
|
|
} else if (strlen(trim($_POST["first_name"])) > 255) {
|
|
$first_name_err = "Too long.";
|
|
} else {
|
|
$first_name = trim($_POST["first_name"]);
|
|
}
|
|
|
|
if (empty(trim($_POST["last_name"]))) {
|
|
$last_name_err = "Please input your last name.";
|
|
} else if (strlen(trim($_POST["last_name"])) > 255) {
|
|
$last_name_err = "Too long.";
|
|
} else {
|
|
$last_name = trim($_POST["last_name"]);
|
|
}
|
|
|
|
// Check input errors before inserting in database
|
|
if (
|
|
empty($username_err) && empty($password_err)
|
|
&& empty($confirm_password_err) && empty($first_name_err)
|
|
&& empty($last_name_err)
|
|
) {
|
|
if (add_user($username, $first_name, $last_name, $password)) {
|
|
// Redirect to login page
|
|
header("location: login.php");
|
|
} else {
|
|
echo "Something went wrong. Please try again later.";
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<!-- TODO : ADD SEO STUFF -->
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
|
<link href="/assets/css/reset.css" rel="stylesheet">
|
|
<link href="/assets/css/common.css" rel="stylesheet">
|
|
<link href="/assets/css/login.css" rel="stylesheet">
|
|
<title>Food invetory - Register</title>
|
|
</head>
|
|
|
|
<body>
|
|
<div class="center30left white">
|
|
<h2 class="montserrat" id="title">Nice to meet you</h2>
|
|
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
|
|
<div class="flex-container flex-content-stretch">
|
|
<input type="email" name="username" class="form-control halo-hover <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>" value="<?php echo $username; ?>" placeholder="E-mail" id="register-email">
|
|
</div>
|
|
<div class="flex-container flex-content-stretch flex-space-between">
|
|
<input type="text" placeholder="First name" name="first_name" class="form-control halo-hover <?php echo (!empty($first_name_err)) ? 'has-error' : ''; ?>" value="<?php echo $first_name; ?>">
|
|
<input type="text" placeholder="Last name" name="last_name" class="form-control halo-hover <?php echo (!empty($last_name_err)) ? 'has-error' : ''; ?>" value="<?php echo $last_name; ?>">
|
|
</div>
|
|
<div class="flex-container flex-content-stretch flex-space-between">
|
|
<input type="password" placeholder="Password" name="password" class="form-control halo-hover <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>" value="<?php echo $password; ?>">
|
|
<input type="password" placeholder="Confirm password" name="confirm_password" class="form-control halo-hover <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>" value="<?php echo $confirm_password; ?>">
|
|
</div>
|
|
<div class="flex-container flex-evenly">
|
|
<div class="error">
|
|
<?php
|
|
if (!empty($username_err)) echo $username_err;
|
|
else if (!empty($first_name_err)) echo $first_name_err;
|
|
else if (!empty($last_name_err)) echo $last_name_err;
|
|
else if (!empty($password_err)) echo $password_err;
|
|
else echo $confirm_password_err;
|
|
?>
|
|
</div>
|
|
</div>
|
|
<div class="form-group flex-container flex-vertical-center flex-space-between">
|
|
<input type="submit" class="halo-hover login-button" value="Submit">
|
|
<a href="login.php" class="login-button halo-hover other-buttons">Already registered? Login!</a>
|
|
<a href="/" class="login-button halo-hover other-buttons">Main page</a>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</body>
|
|
|
|
</html>
|