簡體   English   中英

當用戶登錄PHP時向管理員發送電子郵件

[英]Send an email to admin when user login in PHP

我的腳本中有問題,但是我不確定為什么會發生,這可能是由於會話或HTTP標頭引起的。 我希望我的登錄腳本是這樣的:“當用戶登錄時,應該向用戶發送一封包含用戶信息的電子郵件”,因此,通過排除以下代碼中的“電子郵件”部分,簡單地登錄就可以了,而且我還在單獨的文件中測試了“電子郵件”部分精細。 但是我不為什么不發送電子郵件並且頁面顯示為白色空白屏幕並顯示以下代碼。

session_start();
$errors = array();

if(isset($_POST['submit']) == "Login") {

    if(empty($_POST['username'])) { $errors[] = "Please enter Username! ";} 
    else { $username = mysql_real_escape_string($_POST['username']); }

    if(empty($_POST['password'])) { $errors[] = "Please enter Password! ";} 
    else { $password = mysql_real_escape_string($_POST['password']);}   

    if(empty($errors)) {
        $sql = mysql_query("SELECT * FROM users WHERE `username` = '".$username."' AND `password` = sha1('".$password."')");
         $row = mysql_fetch_assoc($sql);

        if($row) {  
            $_SESSION['user_login'] = true;
            $_SESSION['user_id'] = $row['user_id'];
            $_SESSION['full_name'] = $row['full_name'];

            /*******************Send Email***************************/
            //Send user information to admin            
            $to = "xxx@XXX.com";
            $from = "XXX@xxx.com";

            //Get User Information
            $Full_Name = $row['full_name'];
            $Browser = $_SERVER['HTTP_USER_AGENT'];          
            $User_IP = $_SERVER['REMOTE_ADDR'];
            // If IP address exists
            // Get country (and City) via  api.hostip.info
            if (!empty($User_IP)) {
                $country=file_get_contents('http://api.hostip.info/get_html.php?ip='.$User_IP);
                list ($_country) = explode ("\n", $country);
                $_country = str_replace("Country: ", "", $_country);
            }                        

            $subject = $Full_Name. " logged in!";

            $message = '<html><body>';          
            $message .= '<table rules = "all" border="0" cellpadding="10">';
            $message .= "<tr><td>&nbsp;</td><td>&nbsp;</td></tr>";
            $message .= "<tr style='background: #eee;'><td colspan='2'><strong>Dear Admin!</strong></td></tr>";
            $message .= "<tr><td colspan='2'>A User <strong>" .$Full_Name. "</strong> logged in with following information! </td></tr>";                
            $message .= "<tr><td width='30%'><strong>Name ID:</strong> </td><td>" . $Full_Name  . "</td></tr>";
            $message .= "<tr><td><strong>IP Address:</strong> </td><td>" . $User_IP  . "</td></tr>";
            $message .= "<tr><td><strong>Country:</strong> </td><td>" . $_country  . "</td></tr>";
            $message .= "<tr><td><strong>Browser:</strong> </td><td>" . $Browser  . "</td></tr>";                       
            $message .= "<tr><td>&nbsp;</td><td>&nbsp;</td></tr>";
            $message .= "</table>";             
            $message .= "</body></html>";   
            $headers = "From: " . $from . "\r\n";
            $headers .= "MIME-Version: 1.0\r\n";
            $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
            mail($to, $subject, $message, $headers) or die("Error!");

            echo "<script>window.open('index.php','_self')</script>";

         } else {
            $errors[] = "Incorrect login, please try again!";
         }  
    }#Empty Errors

}#End Submit

基本調試。 在發送電子郵件之前,添加

error_reporting(E_ALL);
ini_set('display_errors', 'on');

然后運行,解決問題,然后在排序后刪除兩行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM