$_SERVER[‘REQUEST_METHOD’]) == "post" affects search results

  contact-form, php, search-form

Hi guys I have a contact form code with recaptcha everything works fine, but it seems like this code if (strtolower($_SERVER['REQUEST_METHOD']) == "post") affects my search form results as well.

Can you please guide me how to refactor my code so it would not affect my search form.

Heres my full contact form code

<?php
session_start();
require_once('vendor/autoload.php');
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;

$from = '<[email protected]>';


if (strtolower($_SERVER['REQUEST_METHOD']) == "post") {
    $response = array(
        'status' => 0,
        'message' => 'Unknown error',
    );

    $context  = stream_context_create(array(
        'http' => array (
            'method' => 'POST',
            'content' => http_build_query(array(
                'secret' => '',
                'response' => $_POST["g-recaptcha-response"]
            ))
        )
    ));
    $verify = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context);
    $captcha_success = json_decode($verify);


    $headers = array('Content-Type: text/plain; charset="UTF-8";',
    'From: ' . $from,
    'Reply-To: ' . $from,
    'Return-Path: ' . $from,
);

  
    if ($captcha_success->success != true) {
        $response['message'] = "reCAPTCHA failure, you seem to be a bot.";
        echo json_encode($response);
        exit();
    }

    $message = $name = $email = $orderno = $phone = $department = null;

    
    $ip_address = $_SERVER['REMOTE_ADDR'];
    $timestamp = date('l, M j Y - h:i a');

    $exclude = array('subject','g-recaptcha-response');
    foreach($_POST as $field => $value) {
        if (!in_array($field, $exclude)) {
            switch ($field) {
                case 'name': $name = $value; break;
                case 'email': $email = $value; break;
                case 'orderno': $orderno = $value; break;
                case 'phone': $phone = $value; break; 
                case 'subject': $department = $value; break;
            }
            $message .= ucwords(str_replace('_', ' ', $field)) . " : " . strip_tags(nl2br($value)) . "<br>";
        }
    }

    


    if (!empty($name) && !empty($email)) {
        $mail = new PHPMailer(); 
        if (array_key_exists('subject', $_POST)) {
            $mail->Subject    = $_POST['subject'];
        }
        $mail->addAddress('[email protected]'); 
        
        $email = $_POST['email'];

        
        $mail->setFrom($email, $name);
        $mail->AddReplyTo($email, $name);
        $mail ->Subject = "Message From Contact Form";
        $mail->Body    = $message;
        $mail->AltBody = strip_tags($message);


        try {
            if ($mail->send()) {
                $response['message'] = "Thank you, your message has been sent. We will contact you back very soon.";
                $response['status'] = 1;
           
            } else {
              
            }
        } catch (Exception $e) {
            $response['message'] = 'Message could not be sent. Mailer Error: '. $mail->ErrorInfo;
        }
    }


    echo json_encode($response);
    exit();
}

?>

when I tried my search form this is the result{"status":0,"message":"reCAPTCHA failure, you seem to be a bot."}

Source: Ask PHP

LEAVE A COMMENT