Loop仅检查一封电子邮件

时间:2017-04-26 12:29:19

标签: php

我们从用户数据库中获取电子邮件地址。然后,我们想要在api中检查这些电子邮件地址并返回违规数量。

问题是,现在看来它只检查第一个电子邮件地址。当我们有多个时,第一个获得正确的违规数量,第二个只获得0.

<?php
ini_set("allow_url_fopen", 1);
include '/home/actiorwd/include/dbinfo.php';

$servername = "localhost";
$dbname = "actiorwd_websec";

// Create connection
$conn = new mysqli($servername, $user, $passw, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
//users contains two columns; email and breaches.
$sql = "SELECT * FROM users";
$result = $conn->query($sql);

$ch = curl_init();

if ($result->num_rows > 0) {
    // output data of each row

    while($row = $result->fetch_assoc()) {
        $url = 'https://haveibeenpwned.com/api/v2/breachedaccount/';
        $url.= $row["email"];

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
        curl_setopt($ch, CURLOPT_URL, $url);
        $results = curl_exec($ch);
        curl_close($ch);
        $obj = json_decode($results, TRUE);

        $usermail = $row["email"];



        $newbreaches = count($obj);

        //run if new breaches are found
        if($row["breaches"]!==$newbreaches){

          require_once('/home/actiorwd/public_html/PHPMailer/PHPMailerAutoload.php');
          $mail = new PHPMailer;

          $mail->isSMTP();                                      // Set mailer to use SMTP
          $mail->Host = 'cpanel40.proisp.no';  // Specify main and backup SMTP servers
          $mail->SMTPAuth = true;                               // Enable SMTP authentication
          $mail->Username = 'breached@actionscript.no';                 // SMTP username
          $mail->Password = 'PASSWORDHERE';                           // SMTP password
          $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
          $mail->Port = 465;                                    // TCP port to connect to

          $mail->setFrom('breached@actionscript.no', 'WebSec');
          $mail->addAddress($row["email"]);     // Add a recipient
          $mail->addReplyTo('breached@actionscript.no', 'WebSec');

          $mail->isHTML(true);                                  // Set email format to HTML

          $mail->Subject = 'Breached!!!!';
          $mail->Body    = 'Someone breached your account, there are: '.$newbreaches."breaches";
          $mail->AltBody = 'Someone breached your account in plain text';
          $mail->send();
          /*
          if(!$mail->send()) {
              echo 'Message could not be sent.';
              echo 'Mailer Error: ' . $mail->ErrorInfo;
          } else {
              echo 'Message has been sent';
          }

          */


          $updatebreach = "INSERT INTO users (`email`, `breaches`) VALUES ('$usermail', '$newbreaches') ON DUPLICATE KEY UPDATE `breaches` = '$newbreaches' ";
          //echo $updatebreach;

          if ($conn->query($updatebreach) === TRUE) {
              //echo "New record created successfully";
          } else {
              //echo "Error: " . $updatebreach . "<br>" . $conn->error;
          }


        }


    }
} else {
    echo "0 results";
}
$conn->close();
?>

1 个答案:

答案 0 :(得分:2)

看看你的循环,或者更好地在你的循环之前的那一行。你在做:

$ch = curl_init(); - 因此您可以开展cURL会话。你这样做只有一次,因为它在循环之外。

然后,几行之后,你正在做:

curl_close($ch); - 您关闭了cURL会话。你在每次通话后都这样做,因为它在你的循环中。所以主要是你初衷,运行cURL的东西(这就是为什么它适用于第一个),然后它停止工作,因为你关闭你的cURL会话,永远不会打开一个新的。

只需从循环中取出curl_close$ch);并将其放在文件的末尾,这样就可以使用相同的会话执行所有请求。然后它应该工作:))

您的文件末尾应如下所示:

}
curl_close($ch);
$conn->close();
?>

希望它有所帮助。