php email code for registration
Posted by Superadmin on September 05 2024 07:58:16

php email code for registration

 

Here’s a simple example of a PHP script for user registration with email verification:

  1. Create a Database Table:

    SQL
    CREATE TABLE `users` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `name` varchar(30) NOT NULL,
        `email` varchar(100) NOT NULL,
        `password` varchar(250) NOT NULL,
        `status` int(11) NOT NULL DEFAULT '0',
        `email_verification_link` varchar(255) NOT NULL,
        `email_verified_at` TIMESTAMP NULL,
        PRIMARY KEY (`id`),
        UNIQUE KEY `email` (`email`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
  2. Database Connection (db.php):

    PHP
    <?php
    $servername = 'localhost';
    $username = 'root';
    $password = '';
    $dbname = 'your_database';
    
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    
    if (!$conn) {
        die('Could not Connect MySql Server:' . mysql_error());
    }
    ?>
    
  3. User Registration Form (index.php):

    PHP
    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <title>User Registration with Email Verification in PHP</title>
    </head>
    <body>
        <div class="container mt-5">
            <div class="card">
                <div class="card-header text-center">
                    User Registration with Email Verification in PHP
                </div>
                <div class="card-body">
                    <form action="store-registration-send-email.php" method="post">
                        <div class="form-group">
                            <label for="name">Name</label>
                            <input type="text" name="name" class="form-control" id="name" required>
                        </div>
                        <div class="form-group">
                            <label for="email">Email address</label>
                            <input type="email" name="email" class="form-control" id="email" required>
                        </div>
                        <div class="form-group">
                            <label for="password">Password</label>
                            <input type="password" name="password" class="form-control" id="password" required>
                        </div>
                        <button type="submit" class="btn btn-primary">Register</button>
                    </form>
                </div>
            </div>
        </div>
    </body>
    </html>
    
  4. Store Registration Data and Send Email (store-registration-send-email.php):

    PHP
    <?php
    include('db.php');
    
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $password = hash('sha256', $_POST['password']);
        $verification_code = md5(rand());
    
        $query = "INSERT INTO users (name, email, password, email_verification_link) VALUES ('$name', '$email', '$password', '$verification_code')";
        $result = mysqli_query($conn, $query);
    
        if ($result) {
            $to = $email;
            $subject = "Email Verification";
            $message = "Click the link below to verify your email address: \n";
            $message .= "http://yourdomain.com/verify-email.php?code=$verification_code";
            $headers = "From: no-reply@yourdomain.com";
    
            if (mail($to, $subject, $message, $headers)) {
                echo "Registration successful, please verify your email.";
            } else {
                echo "Failed to send verification email.";
            }
        } else {
            echo "Failed to register user.";
        }
    }
    ?>
    
  5. Email Verification (verify-email.php):

    PHP
    <?php
    include('db.php');
    
    if (isset($_GET['code'])) {
        $verification_code = $_GET['code'];
        $query = "SELECT * FROM users WHERE email_verification_link='$verification_code'";
        $result = mysqli_query($conn, $query);
    
        if (mysqli_num_rows($result) > 0) {
            $query = "UPDATE users SET status=1, email_verified_at=NOW() WHERE email_verification_link='$verification_code'";
            mysqli_query($conn, $query);
            echo "Email verified successfully.";
        } else {
            echo "Invalid verification code.";
        }
    }
    ?>
    

This example covers the basics of user registration with email verification. Make sure to replace placeholders like your_database and yourdomain.com with your actual database name and domain.