Add registration feature

This commit is contained in:
Andrew Trieu
2023-02-04 15:32:11 +02:00
parent db56a1529d
commit c717c9e4c1
9 changed files with 228 additions and 8 deletions

11
php/config.php Normal file
View File

@@ -0,0 +1,11 @@
<?php
$hostname = "localhost:3200";
$username = "root";
$password = "";
$dbname = "chatline";
$conn = mysqli_connect($hostname, $username, $password, $dbname);
if(!$conn){
echo "Database connection error".mysqli_connect_error();
}
?>

84
php/register.php Normal file
View File

@@ -0,0 +1,84 @@
<?php
session_start();
include_once "config.php";
function escape($conn, $value) {
return mysqli_real_escape_string($conn, $value);
}
function isValidEmail($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
function moveUploadedFile($tempFileName, $newFileName) {
return move_uploaded_file($tempFileName, "images/" . $newFileName);
}
function generateUniqueUserId() {
return rand(time(), 100000000);
}
function encryptPassword($password) {
return md5($password);
}
function fetchUserByEmail($conn, $email) {
$email = escape($conn, $email);
$sql = mysqli_query($conn, "SELECT * FROM users WHERE email = '{$email}'");
return mysqli_fetch_assoc($sql);
}
function insertUser($conn, $unique_id, $first_name, $last_name, $email, $encrypt_pass, $new_img_name, $status) {
return mysqli_query($conn, "INSERT INTO users (unique_id, first_name, last_name, email, password, img, status) VALUES ({$unique_id}, '{$first_name}','{$last_name}', '{$email}', '{$encrypt_pass}', '{$new_img_name}', '{$status}')");
}
function loginUser($user) {
$_SESSION["unique_id"] = $user["unique_id"];
echo "success";
}
if (!empty($_POST["first_name"]) || !empty($_POST["last_name"]) || !empty($_POST["email"]) || !empty($_POST["password"])) {
$first_name = escape($conn, $_POST["first_name"]);
$last_name = escape($conn, $_POST["last_name"]);
$email = escape($conn, $_POST["email"]);
$password = escape($conn, $_POST["password"]);
if (isValidEmail($email)) {
$existingUser = fetchUserByEmail($conn, $email);
if ($existingUser) {
echo "The email address $email already exists!";
} else {
if (isset($_FILES["image"])) {
$img_name = $_FILES["image"]["name"];
$img_type = $_FILES["image"]["type"];
$temp_file_name = $_FILES["image"]["tmp_name"];
$img_ext = pathinfo($img_name, PATHINFO_EXTENSION);
$allowedExtensions = ["jpeg", "png", "jpg"];
if (in_array($img_ext, $allowedExtensions) && in_array($img_type, ["image/jpeg", "image/jpg", "image/png", ])) {
$time = time();
$new_img_name = $time . $img_name;
if (moveUploadedFile($temp_file_name, $new_img_name)) {
$unique_id = generateUniqueUserId();
$status = "Active now";
$encrypt_pass = encryptPassword($password);
} else {
echo "An error occurred while uploading the image.";
exit();
}
} else {
echo "Image must be of the following formats: JPEG, JPG, PNG.";
exit();
}
} else {
$unique_id = generateUniqueUserId();
$status = "Active now";
$encrypt_pass = encryptPassword($password);
}
if (insertUser($conn, $unique_id, $first_name, $last_name, $email, $encrypt_pass, $new_img_name, $status)) {
$loggedInUser = fetchUserByEmail($conn, $email);
if ($loggedInUser) {
loginUser($loggedInUser);
} else {
echo "An error occurred. Please try again.";
}
} else {
echo "An error occurred. Please try again.";
}
}
} else {
echo "The email address $email is not valid!";
}
} else {
echo "All input fields are required!";
}
?>