Kazura92
Kazura92

Reputation: 75

Loop only checks one email

We get email adresses from a database of users. We then want to check these email adresses in a api and return number of breaches.

The problem is that now it seems like it only checks the first email adress. When we have more than one, the first gets the correct number of breaches, the second only gets 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 = '[email protected]';                 // 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('[email protected]', 'WebSec');
          $mail->addAddress($row["email"]);     // Add a recipient
          $mail->addReplyTo('[email protected]', '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();
?>

Upvotes: 0

Views: 53

Answers (1)

Twinfriends
Twinfriends

Reputation: 1997

Look at your loop, or better at the line before your loop. You're doing:

$ch = curl_init(); - so you initalize a cURL session. You do this only once, since its outside of the loop.

Then, a few lines later, youre doing:

curl_close($ch); - you close the cURL session. You do this after every call, because its in your loop. So principally you initalize, run the cURL stuff (thats why it works for the first one) and then it stops to work, because you close your cURL session and never open a new one.

Simply take your the curl_close$ch); from your loop and put it at the end of your file, so you can execute all requests with the same session. Then it should work :)

Your end of the file should look something like:

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

Hope it helped.

Upvotes: 2

Related Questions