2012-09-24 60 views
0

我在脚本中遇到问题,但我不确定它为什么会发生,可能是由于会话或HTTP标头。我希望以这种方式登录脚本“当用户登录时,应该使用用户信息将电子邮件发送给管理员”,因此通过在以下代码中排除电子邮件部分,只需登录工作良好,我也可以在单独的文件中测试电子邮件部分精细。但我不明白为什么电子邮件不发送和页面显示为白色的空白屏幕,下面的代码。用户在PHP中登录时发送电子邮件给管理员

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 = "[email protected]"; 
      $from = "[email protected]"; 

      //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 
+0

可能是因为'echo'“;'? –

+0

好的,但我认为它应该发送电子邮件。 – Bheem

回答

1

基本调试。在您发送电子邮件,添加

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

然后运行,工作出了问题,然后删除这两条线一次排序。

+0

谢谢,是的,它显示我的问题已发送标题。 – Bheem

+0

好东西。一旦解决了问题,不要忘记删除这些调试行 - 您不希望所有人都看到可能会帮助他们为您创建问题的其他错误。 – Robbie

+0

是的,但我忘记了我在制作中,通常会在制作中添加error_reporting(0)。 :) – Bheem

相关问题