Guest User

Untitled

a guest
Aug 1st, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. how to send an email when someone logs into mysql
  2. <?php
  3. session_start();
  4. require_once('connect.php');
  5.  
  6. // Retrieve username and password from database according to user's input
  7. $input_username = mysql_real_escape_string($_POST['username']);
  8. $login = mysql_query("SELECT * FROM user WHERE username = '".$input_username."'" );
  9.  
  10. // Check username and password match
  11. $row = mysql_fetch_array($login);
  12. if (mysql_num_rows($login)) {
  13. if($row['password'] === md5($_POST['password'])){
  14. $_SESSION['username'] = $_POST['username']; // store in session
  15. $sql = "UPDATE user SET logindate = NOW() WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
  16. mysql_query($sql) or die("Error in SQL: " . mysql_error());
  17.  
  18. }
  19. else{
  20. // Invalid login
  21. echo header('Location: loginerror.php');
  22. exit;
  23. }
  24. ?>
  25.  
  26. if ($rowcount == 1) {
  27.  
  28. $_SESSION['username'] = $_POST['username'];
  29.  
  30. $headers = "From:Me <no-reply@example.com>rn";
  31. $headers .= "Reply-To: no-reply@example.comrn";
  32. $email_to = "your@emailadress.tld";
  33. $subject = "Someone logged in!";
  34. $message = "User ".$_POST['username']." logged in!";
  35. mail($email_to, $subject, $message, $headers);
  36.  
  37. header("Location: securedpage.php");
  38.  
  39. }
  40.  
  41. if(mail($email_to, $subject, $message, $headers)) {
  42.  
  43. // mail function was successful
  44.  
  45. } else {
  46.  
  47. // error; mail function was NOT successful
  48.  
  49. }
  50.  
  51. $input_password = mysql_real_escape_string($_POST['password']);
  52.  
  53. $login = mysql_query("SELECT * FROM tbuser WHERE (username = '" . $input_username . "') AND (password = '" . md5($input_password) . "')",$db);
Add Comment
Please, Sign In to add comment