ohctech_p8/api2/email_otp.php
2024-10-16 19:18:52 +05:30

82 lines
3.3 KiB
PHP

<?php
error_reporting(0);
include "../includes/config/config.php";
include "../includes/functions.php";
include "mailend.php";
include 'log_entry.php';
// Include your database connection logic here
// $email = $_REQUEST['email'];
// Include your database connection logic here
$offiial_email_id = $_REQUEST['offiial_email_id'];
error_log("EMAIL : " . $offiial_email_id);
// Check if the email exists in the database
if (!empty($offiial_email_id)) {
$sql = "SELECT * FROM patient_master WHERE offiial_email_id = '$offiial_email_id'";
error_log("API LOGIN DM : " . $sql);
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) == 1) {
// Email exists, generate a new OTP
$otp = rand(100000, 999999);
$expiry = date(strtotime('+5 minutes'));
$sqlcheckotp = "SELECT * FROM email_otp WHERE email_id = '$offiial_email_id'";
error_log("API LOGIN DM : " . $sqlcheckotp);
$resultotp = mysqli_query($conn, $sqlcheckotp);
$otpcount = mysqli_num_rows($resultotp);
if ($otpcount == 1) {
$sql = "UPDATE email_otp SET otp = '$otp', expiry = '$expiry' WHERE email_id = '$offiial_email_id'";
error_log("API LOGIN : " . $sql);
if (mysqli_query($conn, $sql)) {
$massage = "";
$massage .= "<h3>OTP for login <br>Dear customer,<br> use this One Time Password - <b>" . $otp . "</b> to log in to your account. This OTP will be valid for the next 5 mins.</h3>";
$row = mysqli_fetch_assoc($result);
$response = array(
'status' => 'true',
);
echo json_encode($response);
send_mail($offiial_email_id, "Otp for login", $massage, '');
} else {
// Error executing SQL query
echo json_encode(['status' => 'false', 'message' => 'Email does not exist']);
error_log("Dushan jhsj");
}
} else {
$sqlinsert = "INSERT INTO `email_otp`(`email_id`, `otp`, `expiry`) VALUES ('$offiial_email_id', '$otp', '$expiry')";
error_log("API LOGIN : " . $sqlinsert);
if (mysqli_query($conn, $sqlinsert)) {
$massage = "";
$massage .= "<h3>OTP for login <br>Dear customer,<br> use this One Time Password - <b>" . $otp . "</b> to log in to your account. This OTP will be valid for the next 5 mins.</h3>";
$row = mysqli_fetch_assoc($result);
$response = array(
'status' => 'true',
);
echo json_encode($response);
send_mail($offiial_email_id, "Otp for login", $massage, '');
} else {
// Error executing SQL query
echo json_encode(['status' => 'false', 'message' => 'Email does not exist']);
error_log("Error executing SQL query");
}
}
// Update OTP and expiry in the database
} else {
// Email does not exist
echo json_encode(['status' => 'false', 'message' => 'Email does not exist']);
error_log("Email does not exist");
}
} else {
echo json_encode(['status' => 'false', 'message' => 'Email does not exist']);
error_log("Email does not exist 2");
}
?>