Php html email img login

A combination of PHP, HTML, email, and image login!

Here's a basic example of how you can create a login system that sends a verification email with a unique image to the user's email address, and then verifies the image to ensure the user is who they claim to be.

Note: This is a simplified example and should not be used in production without proper security measures and validation.

PHP Code:

<?php

// Configuration
$db_host = 'localhost';
$db_username = 'root';
$db_password = '';
$db_name = 'login_system';

// Connect to database
$conn = new mysqli($db_host, $db_username, $db_password, $db_name);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: ". $conn->connect_error);
}

// Function to generate a unique image
function generate_image() {
    $width = 100;
    $height = 30;
    $image = imagecreate($width, $height);
    $background_color = imagecolorallocate($image, 255, 255, 255);
    $text_color = imagecolorallocate($image, 0, 0, 0);
    $font = 'arial.ttf';
    $text = 'Verification Code';
    $x = 10;
    $y = 10;
    imagettftext($image, 12, 0, $x, $y, $text_color, $font, $text);
    $image_path = 'verification_images/'. time(). '.png';
    imagepng($image, $image_path);
    imagedestroy($image);
    return $image_path;
}

// Function to send verification email
function send_verification_email($email, $image_path) {
    $subject = 'Verify Your Email Address';
    $message = '<p>Please click on the following link to verify your email address:</p>';
    $message.= '<img src="'. $image_path. '" alt="Verification Code">';
    $message.= '<p>If you did not request this email, please ignore it.</p>';
    $headers = 'From: Your Email Address <[email protected]>';
    mail($email, $subject, $message, $headers);
}

// Function to verify image
function verify_image($image_path) {
    $image = imagecreatefrompng($image_path);
    $width = imagesx($image);
    $height = imagesy($image);
    $code = '';
    for ($x = 0; $x < $width; $x++) {
        for ($y = 0; $y < $height; $y++) {
            $pixel = imagecolorat($image, $x, $y);
            $code.= chr($pixel);
        }
    }
    imagedestroy($image);
    return $code;
}

// Login form submission
if (isset($_POST['submit'])) {
    $email = $_POST['email'];
    $password = $_POST['password'];
    $query = "SELECT * FROM users WHERE email = '$email' AND password = '$password'";
    $result = $conn->query($query);
    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $image_path = generate_image();
        send_verification_email($email, $image_path);
        $_SESSION['verified'] = false;
        header('Location: verify.php');
        exit;
    } else {
        echo 'Invalid email or password';
    }
}

// Verification page
if (isset($_GET['verify'])) {
    $code = $_GET['code'];
    $image_path = 'verification_images/'. $code. '.png';
    if (file_exists($image_path)) {
        $verified_code = verify_image($image_path);
        if ($verified_code == $code) {
            $_SESSION['verified'] = true;
            header('Location: login.php');
            exit;
        } else {
            echo 'Invalid verification code';
        }
    } else {
        echo 'Verification code not found';
    }
}

// Login page
if (!isset($_SESSION['verified'])) {
   ?>
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
        <label for="email">Email:</label>
        <input type="email" id="email" name="email"><br><br>
        <label for="password">Password:</label>
        <input type="password" id="password" name="password"><br><br>
        <input type="submit" name="submit" value="Login">
    </form>
    <?php
}

// Logout page
if (isset($_SESSION['verified'])) {
   ?>
    <p>Welcome, <?php echo $_SESSION['email'];?></p>
    <a href="<?php echo $_SERVER['PHP_SELF'];?>?logout">Logout</a>
    <?php
    if (isset($_GET['logout'])) {
        session_destroy();
        header('Location: login.php');
        exit;
    }
}

$conn->close();

?>

HTML Code:

<!-- login.php -->
<!DOCTYPE html>
<html>
<head>
    <title>Login System</title>
</head>
<body>
    <?php if (!isset($_SESSION['verified'])) {?>
        <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
            <label for="email">Email:</label>
            <input type="email" id="email" name="email"><br><br>
            <label for="password">Password:</label>
            <input type="password" id="password" name="password"><br><br>
            <input type="submit" name="submit" value="Login">
        </form>
    <?php } else {?>
        <p>Welcome, <?php echo $_SESSION['email'];?></p>
        <a href="<?php echo $_SERVER['PHP_SELF'];?>?logout">Logout</a>
    <?php }?>
</body>
</html>

Verification Page:

<!-- verify.php -->
<?php
if (isset($_GET['code'])) {
    $code = $_GET['code'];
    $image_path = 'verification_images/'. $code. '.png';
    if (file_exists($image_path)) {
        $verified_code = verify_image($image_path);
        if ($verified_code == $code) {
            $_SESSION['verified'] = true;
            header('Location: login.php');
            exit;
        } else {
            echo 'Invalid verification code';
        }
    } else {
        echo 'Verification code not found';
    }
}
?>

How it works:

  1. The user submits the login form with their email and password.
  2. The PHP script checks if the email and password are valid, and if so, generates a unique image and sends a verification email to the user's email address.
  3. The user receives the email and clicks on the verification link, which contains the unique image.
  4. The PHP script verifies the image by checking if the image exists and if the code embedded in the image matches the code sent in the email.
  5. If the verification is successful, the user is logged in and redirected to the login page.
  6. The user can then log out by clicking on the logout link.

Security Notes: