Skip to content

Commit

Permalink
registration/signup feature completed!
Browse files Browse the repository at this point in the history
  • Loading branch information
kuldp18 committed Dec 30, 2023
1 parent 662acc9 commit 4f38760
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 17 deletions.
6 changes: 5 additions & 1 deletion controllers/register.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ function is_username_taken(object $pdo, string $username){
}

function is_email_registered(object $pdo, string $email){
if(get_email($pdo, $email)){
if(get_email($pdo, $email)){
return true;
}
return false;
}

function create_user(object $pdo,string $fullname, string $email, string $username, string $password){
set_user($pdo, $fullname, $email, $username, $password);
}
14 changes: 14 additions & 0 deletions includes/register.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,24 @@

if($errors){
$_SESSION["errors_register"] = $errors;
$reg_data = [
"fullname" => $fullname,
"email" => $email,
"username" => $uname
];
$_SESSION["reg_data"] = $reg_data;

header('Location: ../pages/register.php');
die();
}

// register user
create_user($pdo, $fullname, $email, $uname, $pass);
header('Location: ../pages/login.php?signup=success');

$pdo = null;
$stmt = null;


} catch (PDOException $e) {
die("Query failed: " . $e->getMessage());
Expand Down
13 changes: 13 additions & 0 deletions models/register.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,17 @@ function get_email(object $pdo, string $email){

$result = $stmt->fetch(PDO::FETCH_ASSOC); //fetch the result from the query
return $result;
}

function set_user(object $pdo,string $fullname, string $email, string $username, string $password){
$query = "INSERT INTO users (full_name, email, username, password) VALUES (:fullname, :email, :username, :password)";
$stmt = $pdo->prepare($query); // prepare the query

$options = ['cost' => 12];
$hashed_password = password_hash($password, PASSWORD_BCRYPT, $options);
$stmt->bindParam(":fullname", $fullname, PDO::PARAM_STR); // bind the $fullname variable to the :fullname placeholder
$stmt->bindParam(":email", $email, PDO::PARAM_STR); // bind the $email variable to the :email placeholder
$stmt->bindParam(":username", $username, PDO::PARAM_STR); // bind the $username variable to the :username placeholder
$stmt->bindParam(":password", $hashed_password, PDO::PARAM_STR); // bind the $hashed_password variable to the :password placeholder
$stmt->execute(); // execute the query
}
6 changes: 6 additions & 0 deletions pages/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,11 @@
<input type="submit" value="Login" class="login__btn" />
</form>
</main>

<?php
if(isset($_GET["signup"]) && $_GET["signup"] === "success"){
echo "<p style='color:white, font-size: 1.5rem'>Registration was successful!</p>";
}
?>
</body>
</html>
19 changes: 3 additions & 16 deletions pages/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,9 @@
<a href="./login.php" class="registration__link">here</a>.
</h3>
<form class="registration__form" method="post" action="../includes/register.inc.php">
<input
type="text"
name="fullname"
placeholder="Enter full name"
/>
<input type="email" name="email" placeholder="Enter email" />
<input
type="text"
name="username"
placeholder="Enter username"
/>
<input
type="password"
name="password"
placeholder="Enter password"
/>
<?php
register_inputs();
?>
<input type="submit" value="Register" class="registration__btn" />
</form>
</main>
Expand Down
54 changes: 54 additions & 0 deletions views/register.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,60 @@

declare(strict_types=1);

function register_inputs(){
// fullname

if(isset($_SESSION["reg_data"]["fullname"])){
echo '<input
type="text"
name="fullname"
placeholder="Enter full name"
value="'.$_SESSION["reg_data"]["fullname"].'"/>';
}else{
echo '<input
type="text"
name="fullname"
placeholder="Enter full name"/>';
}

// email
if(isset($_SESSION["reg_data"]["email"]) && !isset($_SESSION["errors_register"]["email_taken"]) && isset($_SESSION["reg_data"]["email"]) && !isset($_SESSION["errors_register"]["invalid_email"])){
echo ' <input
type="email"
name="email"
placeholder="Enter email"
value="'.$_SESSION["reg_data"]["email"].'"/>';
}else{
echo ' <input
type="email"
name="email"
placeholder="Enter email"
/>';
}

//username
if(isset($_SESSION["reg_data"]["username"]) && !isset($_SESSION["errors_register"]["username_taken"])){
echo ' <input
type="text"
name="username"
placeholder="Enter username"
value="'.$_SESSION["reg_data"]["username"].'"/>';
}else{
echo ' <input
type="text"
name="username"
placeholder="Enter username"
/>';
}

// password
echo '<input
type="password"
name="password"
placeholder="Enter password"
/>';
}

function check_and_print_register_errors(){
if(isset($_SESSION["errors_register"])){
$errors = $_SESSION["errors_register"];
Expand Down

0 comments on commit 4f38760

Please sign in to comment.