top of page

Group

Public·21 members

PHP Login And Registration System €? Email Confirm Activation


PHP Login and Registration System Email Confirm Activation




In this article, we will learn how to create a PHP login and registration system that requires users to confirm their email address before they can access the website. This is a common feature in many web applications, as it helps to verify the identity of the users and prevent spam accounts.


Download File: https://tercdipcredto.blogspot.com/?download=2w3WZB


To implement this feature, we will need the following components:


  • A database to store the user information and the activation codes.



  • A PHP script to handle the user registration, email confirmation, and login processes.



  • A HTML form to collect the user input and display the messages.



  • A SMTP server to send the confirmation emails to the users.



Let's see how each component works in detail.


Database




We will use MySQL as our database system, and create a table named users with the following fields:


FieldTypeDescription


idINTThe primary key of the table, auto-incremented.


usernameVARCHAR(50)The username of the user, unique and not null.


passwordVARCHAR(255)The hashed password of the user, not null.


emailVARCHAR(100)The email address of the user, unique and not null.


statusTINYINT(1)The activation status of the user, 0 for inactive and 1 for active.


codeVARCHAR(255)The activation code of the user, generated randomly.


We can create this table using the following SQL statement:


```sql CREATE TABLE users ( id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50) UNIQUE NOT NULL, password VARCHAR(255) NOT NULL, email VARCHAR(100) UNIQUE NOT NULL, status TINYINT(1) DEFAULT 0, code VARCHAR(255) ); ``` PHP Script




We will create a PHP script named user.php that will perform the following tasks:


  • Connect to the database using PDO (PHP Data Objects).



  • Define some constants and variables for the database credentials, SMTP server settings, and error messages.



  • Create a function named register() that will take the user input from the registration form, validate it, insert it into the database, generate an activation code, send an email to the user with the activation link, and display a success or error message.



  • Create a function named activate() that will take the activation code from the URL, verify it with the database, update the status of the user to active, and display a success or error message.



  • Create a function named login() that will take the user input from the login form, validate it, check it with the database, start a session for the user if successful, and display a success or error message.



  • Create a function named logout() that will end the session for the user and redirect them to the login page.



  • Create a function named send_email() that will use PHPMailer library to send an email to the user with the activation link.



  • Create a function named hash_password() that will use password_hash() function to hash the password using BCRYPT algorithm.



  • Create a function named verify_password() that will use password_verify() function to verify the password with the hashed password stored in the database.



  • Create a function named generate_code() that will use random_bytes() and bin2hex() functions to generate a random hexadecimal string as the activation code.



  • Create a function named redirect() that will use header() function to redirect the user to another page.



  • Create a function named alert() that will use echo() function to display an alert message using Bootstrap classes.



  • Create some conditional statements to check if the user has submitted any forms or clicked any links, and call the appropriate functions accordingly.



The complete code of user.php is as follows:


```php setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); catch (PDOException $e) die("Connection failed: " . $e->getMessage()); // Define some constants and variables define("SITE_URL", " // The URL of the site define("SMTP_HOST", "smtp.gmail.com"); // The SMTP host define("SMTP_PORT", 587); // The SMTP port define("SMTP_USER", "example@gmail.com"); // The SMTP username define("SMTP_PASS", "password"); // The SMTP password define("SMTP_FROM", "example@gmail.com"); // The SMTP from address define("SMTP_NAME", "Login System"); // The SMTP from name $success = ""; // The success message $error = ""; // The error message // Create a function to register a user function register($username, $password, $email) // Create a function to activate a user function activate($code) global $db, $success, $error; // Validate the activation code if (empty($code)) redirect(SITE_URL); exit; // Check if the activation code exists in the database $stmt = $db->prepare("SELECT * FROM users WHERE code = ?"); $stmt->execute([$code]); if ($stmt->rowCount() == 0) redirect(SITE_URL); exit; // Update the status of the user to active and clear the code $stmt = $db->prepare("UPDATE users SET status = ?, code = ? WHERE code = ?"); if ($stmt->execute([1, null, $code])) $success = "Email confirmed. You can now login."; else $error = "Failed to confirm email."; // Create a function to login a user function login($username, $password) global $db, $success, $error; // Validate the user input if (empty($username) // Create a function to logout a user function logout() // End the session for the user and destroy it session_start(); session_unset(); session_destroy(); // Redirect the user to the login page redirect(SITE_URL); // Create a function to send an email to the user with the activation link function send_email($to, $subject, $body) // Include the PHPMailer library require_once "PHPMailer/PHPMailer.php"; require_once "PHPMailer/SMTP.php"; require_once "PHPMailer/Exception.php"; // Create a new PHPMailer object $mail = new PHPMailer\PHPMailer\PHPMailer(); // Set the SMTP settings $mail->isSMTP(); $mail->Host = SMTP_HOST; $mail->Port = SMTP_PORT; $mail->SMTPAuth = true; $mail->Username = SMTP_USER; $mail->Password = SMTP_PASS; $mail->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS; // Set the email headers $mail->setFrom(SMTP_FROM, SMTP_NAME); $mail->addAddress($to); $mail->isHTML(true); $mail->Subject = $subject; $mail->Body = $body; // Send the email and return the result return $mail->send(); // Create a function to hash the password using BCRYPT algorithm function hash_password($password) return password_hash($password, PASSWORD_BCRYPT); // Create a function to verify the password with the hashed password stored in the database function verify_password($password, $hash) return password_verify($password, $hash); // Create a function to generate a random activation code function generate_code() return bin2hex(random_bytes(16)); // Create a function to redirect the user to another page function redirect($url) header("Location: $url"); // Create a function to display an alert message using Bootstrap classes function alert($type, $message) echo "$message"; // Check if the user has submitted the registration form if (isset($_POST['register'])) // Get the user input from the form $username = $_POST['username']; $password = $_POST['password']; $email = $_POST['email']; // Call the register function with the user input register($username, $password, $email); // Check if the user has clicked the activation link if (isset($_GET['action']) && $_GET['action'] == 'activate') // Get the activation code from the URL $code = $_GET['code']; // Call the activate function with the activation code activate($code); // Check if the user has submitted the login form if (isset($_POST['login'])) // Get the user input from the form $username = $_POST['username']; $password = $_POST['password']; // Call the login function with the user input login($username, $password); // Check if the user has clicked the logout link if (isset($_GET['action']) && $_GET['action'] == 'logout') // Call the logout function logout(); My mistake, I cant give a response to that right now. Lets try a different topic. My mistake, I cant give a response to that right now. Lets try a different topic.


About

Welcome to the group! You can connect with other members, ge...
Group Page: Groups_SingleGroup
bottom of page