Login Alert Notification using PHP - PHPMailer

Generate an Email Alert upon Login

 
      Login Page Code 

  <?php
  session_start();
  ?>
  <!DOCTYPE html>
  <html lang="en">
  <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Login Page</title>
  </head>
  <body>
      <form action="test.php" method="post">
       <label for="userid">Enter your UserId</label>
       <input type="text" name="userid" id="userid"> <br>
       <label for="password"> Enter your Password</label>
       <input type="text" name="password" id="password"><br>
       <input type="submit" value="submit" name="submit">
      </form>
  <?php
  include "db.php";
  require "vendor/autoload.php";
  use PHPMailer\PHPMailer\PHPMailer;
  use PHPMailer\PHPMailer\SMTP;
  if(isset($_POST['submit']))
  {
      $userid = $_POST['userid'];
      $password = $_POST['password'];
      $conn = OpenCon();
      echo "connection opened successfully";
      //we need to fetch the password from the database for the userid that
      //is entered in the form 
      $stmt=$conn->prepare("select userid,password,email,username from user where userid=?");
      $stmt->bind_param('s',$userid);
      $stmt->execute();
      $result = $stmt->get_result();
      echo $result->num_rows;
      if($result->num_rows > 0)
      {
          while($row = $result->fetch_assoc())
          {
              if(strcmp($row['userid'],$userid)==0 &&  strcmp($row['password'],$password)==0)
             {            
              $mail = new PHPMailer(true);
                         
              $mail->isSMTP();
              $mail->SMTPAuth = true;
              
              $mail->Host = "smtp.gmail.com";
              $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
              $mail->Port = 587;
              
              $mail->Username = "satishnevergivesup@gmail.com";
              $mail->Password = "ylyzlyrtoqqvpbli";
              
              $mail->setFrom("satishnevergivesup@gmail.com", 'CodeSpindle');
              $mail->addAddress($row['email'], $row['username']);
              $mail->Subject = "Security Alert";
              date_default_timezone_set('Asia/Kolkata'); 
              $mail->Body = "You have Logged in your Application ".date("Y-m-d H:i:s");
              $mail->send();
              header("Location: HomePage.php");
             }else
             {
                 echo "Invalid user credentials. Kindly check your credentials";
             }
          }
      }else
      {
        echo "No such user found in our database";
      }
      CloseCon($conn);
   }
  ?>
  </body>
  
  </html>

Homepage Code 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Home Page</title>
</head>
<body>
    <h1>Welcome to CodeSpindle.com.</h1>
    <h2>Check if you have received an email now</h2>
</body>
</html>
    
Output

Enter the Program title here

 
      copy and paste the code here
    
Output