如果有人登录数据库,我希望发送一封电子邮件给我。我的连接和登录配置如下。
<?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;
}
?>
答案 0 :(得分:0)
您只需使用PHP mail功能发送邮件即可。
如果您想在每次用户登录时收到电子邮件,您可能希望像这样添加:
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");
}
要检查邮件功能是否成功,您可以使用:
if(mail($email_to, $subject, $message, $headers)) {
// mail function was successful
} else {
// error; mail function was NOT successful
}
修改强>
只需注意:您执行两次查询,而在第二次查询中则不使用转义数据。您应该删除第一个并将第二个更改为下面的代码。也可以使用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);