[英]how to send an email when someone logs into mysql
I would like an email sent to me if a someone has logged into the database. 如果有人登录数据库,我希望收到一封电子邮件。 My config to connect and log in is below.
我的连接和登录配置如下。
<?php
session_start();
require_once('connect.php');
// Retrieve username and password from database according to user's input
$input_username = mysql_real_escape_string($_POST['username']);
$login = mysql_query("SELECT * FROM user WHERE username = '".$input_username."'" );
// Check username and password match
$row = mysql_fetch_array($login);
if (mysql_num_rows($login)) {
if($row['password'] === md5($_POST['password'])){
$_SESSION['username'] = $_POST['username']; // store in session
$sql = "UPDATE user SET logindate = NOW() WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
mysql_query($sql) or die("Error in SQL: " . mysql_error());
}
else{
// Invalid login
echo header('Location: loginerror.php');
exit;
}
?>
You can simply send mails with the PHP mail function. 您只需使用PHP 邮件功能发送邮件即可。
You may want to add it like this, if you want to recieve an email every time a user logged in: 如果您希望每次用户登录时都收到一封电子邮件,则可能需要这样添加:
if ($rowcount == 1) {
$_SESSION['username'] = $_POST['username'];
$headers = "From:Me <no-reply@example.com>\r\n";
$headers .= "Reply-To: no-reply@example.com\r\n";
$email_to = "your@emailadress.tld";
$subject = "Someone logged in!";
$message = "User ".$_POST['username']." logged in!";
mail($email_to, $subject, $message, $headers);
header("Location: securedpage.php");
}
To check whether the mail function was successful or not you can use: 要检查邮件功能是否成功,可以使用:
if(mail($email_to, $subject, $message, $headers)) {
// mail function was successful
} else {
// error; mail function was NOT successful
}
Edit: 编辑:
Just a note: You do a query twice and in the second one you don't use escaped data. 请注意:您执行了两次查询,而在第二次查询中,您不使用转义数据。 You should remove your first one and change the second one to the code below.
您应该删除第一个,然后将第二个更改为下面的代码。 Also use mysql_real_escape string for the password:
还要使用mysql_real_escape字符串作为密码:
$input_password = mysql_real_escape_string($_POST['password']);
$login = mysql_query("SELECT * FROM tbuser WHERE (username = '" . $input_username . "') AND (password = '" . md5($input_password) . "')",$db);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.