Home > OS >  "Array" (the word) written at end of file... splitting lines into arrays and then writing
"Array" (the word) written at end of file... splitting lines into arrays and then writing

Time:12-07

Ok, I give up. Gurus of the mountain, please help?

I have a script that connects to a gmail account, extracts IP addresses, and writes them to a flat file my PHP header denies site access by IP (email form spammers).

The problem is, at the end of my file, after a bunch of IPs, I get the word "Array" (shown below)

99.154.188.141
94.180.207.5Array



$ips = $this->server->fetchEmails($this->checkEmailCriteria, [$this->parser, 'getIPAddress'], [$this, 'notifyAdmin']);
if ($ips) {
    $this->updateBlockList($ips);
}


public function fetchEmails($criteria, $filter, $success) {
    $inbox = new PHPMailer();
    $inbox = imap_open(
        "{imap.gmail.com:993/imap/ssl}" . $this->gmailInboxFolder,
        $this->gmailUsername,
        $this->gmailPassword,
        FT_PEEK
    ) or die($this->strConnectionFailed . imap_last_error());
    $emails = imap_search($inbox, $criteria, SE_UID);

    $found = [];
    if ($emails) {
        foreach ($emails as $id) {
            $message = imap_fetchbody($inbox, $id, 1, FT_UID);
            $message = quoted_printable_decode(trim($message));
            $message = html_entity_decode($message);
            $ip = $filter($message);
            if ($ip) {
                echo $ip;
                array_push($found, $IP);
            }
        }
    }
    imap_close($inbox, CL_EXPUNGE);
    return $found;
}

private function updateBlockList($ips) {
    $blockedIPs = file($this->blockFile);
    $blockedIPs = array_merge($blockedIPs, [$ips]);
    $blockedIPs = array_filter($blockedIPs);
    $blockedIPs = array_unique($blockedIPs);
    sort($blockedIPs);
    $this->writeFile(
        $this->blockFile,
        $blockedIPs
    );
}


private function writeFile($file, $contents) {
    file_put_contents(
        $file,
        $contents
    );
}

CodePudding user response:

I think this is the part of the problem that puts the word "Array" at the end of the file.

$blockedIPs = array_merge($blockedIPs, [$ips]);

$ips is already an array, but you're putting it inside another array before merging it.

But also note what aynber said in the comments. I think the $ips array is probably empty because of that variable naming issue.

  •  Tags:  
  • php
  • Related