Fatal Error Just cannot figure this one out

I keep getting this error:

[23-Feb-2019 21:50:56 UTC] PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65011720 bytes) in /home/***/public_html/connect/send.php on line 150
I cannot figure it out. And unfortunately my lame-ass hosting service will not allow me to install xdebug, so I was wondering if anyone could help. It’s a basic form script. The form page is:

<?php
// Variables
global $mobile, $title, $description, $timestamp, $time, $date;
$alert = "";
$error = "";

unset($_GET);
unset($_POST);
unset($_SERVER);
unset($errors);

// Error handling
if (isset($_GET['error']))
{   
    include "error.php";
}

?>

    <br><br><br>
    <div class="content"
        <div class="contact">
            <form method="POST" action="<?php echo WWW; ?>/connect/send.php" class="contact" enctype="multipart/form-data">
                <p>
                    <input type="text" id="fname" name="fname" placeholder="First Name" required>
                    <input type="text" id="lname" name="lname" placeholder="Last Name" required>
                </p>
                <p>
                    <input type="text" id="company" name="company" placeholder="Company">
                    <input type="tel" id="phone" name="phone" placeholder="Phone" required>
                    <?php if(isset($_GET['phone'])) echo "<span class='error'>Invalid phone number.</span>"; ?>
                </p>
                <p>
                    <input type="email" id="email" name="email" placeholder="Email" required>
                    <?php if(isset($_GET['email'])) echo "<span class='error'>Invalid email.</span>"; ?>
                    <input type="url" id="website" name="website" placeholder="Website">
                    <?php if(isset($_GET['website'])) echo "<span class='error'>Invalid URL.</span>"; ?>
                </p>
                <p>
                    <textarea id="message" name="message" placeholder="Please elaborate. . ." style="height:200px"></textarea>
                </p>
                    <center><input type="submit" name="submit" value="Submit"></center>
            </form>
        </div>
    </div>
    <br><br>

?>

The send script is this: <?php
// Functions
require "functions.php";
require "PHPMailer/PHPMailer.php";
require "PHPMailer/Exception.php";

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Variables
global $alert, $error, $timestamp, $time, $date;

// Form
$fname = $_POST['fname'];                           // required
$lname = $_POST['lname'];                           // required
$name = $fname . " " . $lname;
$company = $_POST['company'];                       // not required
$phone = $_POST['phone'];                           // required
$email = $_POST['email'];                           // required
$website = $_POST['website'];                       // not required
$message = $_POST['message'];                       // required

// Email
$response = "";
$ip = $_SERVER['REMOTE_ADDR'];                      // client's IP address
$mask = $_SERVER['HTTP_X_FORWARDED_FOR'];           // additional IP information
$browser = $_SERVER['HTTP_USER_AGENT'];             // client's browser
$referer = $_SERVER['HTTP_REFERER'];                // referral page
$method = $_SERVER['REQUEST_METHOD'];               // method used (GET, POST, etc.)
$path = $_SERVER['PATH_INFO'];                      // path info
$from = "[email protected]";
$subject = "CONNECT :: $name, $company | $service $response";
$e_message = "";
$headers = "";
$query = "";
$thanks = "https:/mywebsite.com/connect/thanks.php";

// Other
$_SESSION['name'] = $name;

if (isset($_POST['submit']))
{
    if (empty($_POST['fname']) || empty($_POST['lname']) || empty($_POST['email']) || empty($_POST['phone']) || empty($_POST['message']))
    {
        $errors[] = "empty";
    }
    if (!preg_match("/^[A-Za-z .'-]+$/", $fname))
    {
        $errors[] = "fname";
    }
    if (!preg_match("/^[A-Za-z .'-]+$/", $lname))
    {
        $errors[] = "lname";
    }
    if (!filter_var($email, FILTER_VALIDATE_EMAIL))
    {
        $errors[] = "email";
    }
    /* if (!preg_match("/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/", $phone))
    {
        $errors[] = "phone";
    } */
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $website))
    {
        $errors[] = "website";
    }
    if (strlen($message) < 2)
    {
        $errors[] = "message";
    }
    if (empty($errors))
    {
        if (isset($_POST['message']))
        {
            $response = "w/ Message";
        }

        $e_message .= "<html><body><center>";
        $e_message .= "<br><b>Sent:</b> $timestamp | <b>IP Address:</b> $ip / $mask</p>";
        $e_message .= "<p><b>" . $name . "</b>, <i>" . $company . "</i></p>";
        $e_message .= "<br><b>Phone:</b> <a href='tel:" . $phone . "'>" . $phone . "</a>\n";
        $e_message .= "<br><b>Email:</b> <a href='mailto:" . $email . "'>" . $email . "</a>\n";
        $e_message .= "<br><b>Website:</b> <a href='" . $website . "'>" . $website . "</a></body></html>\n";

        $headers    = "From: " . $from . "\r\n" . "Reply-To: " .$email . "\r\n" . "X-Mailer: PHP/" . phpversion();

        $mail = new PHPMailer(true);
        $mail->Host = 'relay-hosting.myhost.net';
        $mail->SetFrom('[email protected]', 'Info');
        $mail->addAddress('[email protected]', 'Webmaster');
        $mail->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";};
        $mail->Debugoutput = 'echo';
        $mail->IsHTML(true);

        $mail->Subject = $subject;
        $mail->Body = $e_message;

        if(!$mail->send())
        {
            $error[] = "server";
            $mail_error = $mail->ErrorInfo;

            error_log($mail_error, 1, "[email protected]");
            insert("INSERT INTO errors (section, page, error, time, ip, browser, referrer, method, path) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)", array('connect', 'index.php', '$timestamp', '$mail_error', '$ip', '$browser', '$referrer', '$method', '$path'));

            foreach ($errors as $error)
            {
                while (count($errors) > 1)
                {
                    $query .= $error . "&";
	            }
	            $query .= $error;
            }
            $url = "https://mywebsite.com/connect/index.php?error=TRUE&" . $query;
            print "<meta http-equiv=\"refresh\" content=\"0;URL=$url\">";
        } 
            else
            {
                insert("INSERT INTO connect (date, name, info, phone, email, website, referral, service_requested, message, ip, browser) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", array('$date', '$name', '$info', '$phone', '$email', '$website', '$referral', '$service', '$message', '$ip', '$browser'));
                print "<meta http-equiv=\"refresh\" content=\"0;URL=$thanks\">";
            }
    }
        else
        {
            foreach ($errors as $error)
            {
                while (count($errors) > 1)
                {
                    $query .= $error . "&";
                }
                $query .= $error;
            }
            $url = "https://mywebsite.com/connect/index.php?error=TRUE&" . $query;
            print "<meta http-equiv=\"refresh\" content=\"0;URL=$url\">";
        }
}

?>

This script was working fine, and then all of a sudden I get this message. Which is weird, because when I use another script I made that creates thumbnails from image files and logs all the info into my MySQL database, which sometimes can be a 100 entries at once, this doesn’t happen. Yet trying to send four $_POST contents somehow crashes everything.

The two while(){} loops like this - while (count($errors) > 1) will never be false when there’s an error, will loop forever, and will consuming all available memory. You were probably not getting any $errors before, so that code never executed.

To build the query string part of a url, use php’s http_build_query() function or better yet put the form and the form processing code on the same page so you aren’t redirecting all over your site and can directly display any errors when you (re)display the form and (re)populate the form fields with the existing values.

Ah yes that was dumb of me. I appreciate your help that definitely did it.

Sponsor our Newsletter | Privacy Policy | Terms of Service