-1

the contact form on the webpage I am working on is throwing an error. Below is the code and the error log. Any ideas?

<?php

    function utf8_urldecode($str) { $str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($str)); return html_entity_decode($str,null,'ISO-8859-1');; } 

    function mail_utf8($to, $subject = '(No subject)', $message, $headers) {
    $header_ = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=UTF-8' . "\r\n";
    return mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_ . $headers);
    }

    //function mail_utf8html($to, $subject = '(No subject)', $message, $headers) {
    //$header_ = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";
    //return mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_ . $headers);
    //}


    $stackid = trim($_POST['stackid']);
    $cleanowneremail = include(dirname(__FILE__) . '/../'.$stackid.'config_e.php');

    $emaillabel = strip_tags($_POST['emaillabel']);
    $namelabel = strip_tags($_POST['namelabel']);
    $subjectlabel = strip_tags($_POST['subjectlabel']);
    $messagelabel = strip_tags($_POST['messagelabel']);

    $mail = $_POST['mail'];
    $name = strip_tags($_POST['name']);
    $subject = strip_tags($_POST['subject']);
    $text = trim($_POST['text']);
    $text = utf8_urldecode($text);
    //$text = stripslashes($text);

    //$owneremail = $_POST['owneremail'];
    //$cleanowneremail = substr($owneremail, 7); // removes "obscure"




    $recipientEmail = explode(',', $cleanowneremail);
    foreach($recipientEmail as $value) {
    $to = trim($value);
    $subject = stripslashes($subject);
    $message = trim($emaillabel)." : ".trim($mail)."\n".trim($namelabel)." : ".trim($name)."\n".trim($subjectlabel)." : ".trim($subject)."\n ".trim($messagelabel)." : ".$text."\n\nForm = ".trim($_POST['formname']);
    $message = stripslashes($message);
    if(trim($_POST['compatibility']) == "true"){
        $headers = "From: " . $cleanowneremail . "\n";
    }else{
        $headers = "From: " . $mail . "\n";
    }
    $headers .= "Reply-To: ". $mail . "\n";
    if(mail_utf8($to,$subject,$message,$headers)){
    echo "success"; // we should get this echoed back
    }
    else{
    echo "error";
    }
        }




    $autoreply = trim($_POST['autoreply']);
    $autoreply = utf8_urldecode($autoreply);
    $autoreplysig = $_POST['autoreplysig']; // if auto reply is selected in the stack
    $autoreplysub = $_POST['autosubject'];
    if($autoreply != "noreply"){
    $autoreplysig = utf8_urldecode($autoreplysig);
    $autoreply = stripslashes($autoreply)."<br /><br />".stripslashes($autoreplysig);
    //$autoreply .= "<br /><br />Your Message : ".$text;
    $autoreplysubject = utf8_urldecode($autoreplysub);
    $headers = "From: " . $cleanowneremail . "\r\n";
    $headers .= "Reply-To: ". $cleanowneremail . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    mail($mail,$autoreplysubject,$autoreply,$headers);
    }

?>

Error log

  • [22-Jan-2020 20:28:14 UTC] PHP Notice: Undefined index: stackid in
    /home/e5z2aaq161fd/public_html/contactus/files/contactAssets/contactform.php on line 15
  • [22-Jan-2020 20:28:14 UTC] PHP Warning:
    include(/home/e5z2aaq161fd/public_html/contactus/files/contactAssets/../config_e.php): failed to open stream: No such file or directory in
    /home/e5z2aaq161fd/public_html/contactus/files/contactAssets/contactform.php on line 16
  • [22-Jan-2020 20:28:14 UTC] PHP Warning: include(): Failed opening '/home/e5z2aaq161fd/public_html/contactus/files/contactAssets/../config_e.php' for inclusion (include_path='.:/opt/alt/php72/usr/share/pear') in
    /home/e5z2aaq161fd/public_html/contactus/files/contactAssets/contactform.php on line 16
  • [22-Jan-2020 20:28:14 UTC] PHP Notice: Undefined index:
    emaillabel in
    /home/e5z2aaq161fd/public_html/contactus/files/contactAssets/contactform.php on line 19
  • [22-Jan-2020 20:28:14 UTC] PHP Notice: Undefined index: namelabel in
    /home/e5z2aaq161fd/public_html/contactus/files/contactAssets/contactform.php on line 20
  • [22-Jan-2020 20:28:14 UTC] PHP Notice: Undefined index:
    subjectlabel in
    /home/e5z2aaq161fd/public_html/contactus/files/contactAssets/contactform.php on line 21
  • [22-Jan-2020 20:28:14 UTC] PHP Notice: Undefined index:
    messagelabel in
    /home/e5z2aaq161fd/public_html/contactus/files/contactAssets/contactform.php on line 22
  • [22-Jan-2020 20:28:14 UTC] PHP Notice: Undefined index: formname in
    /home/e5z2aaq161fd/public_html/contactus/files/contactAssets/contactform.php on line 41
  • [22-Jan-2020 20:28:14 UTC] PHP Notice: Undefined index:
    compatibility in
    /home/e5z2aaq161fd/public_html/contactus/files/contactAssets/contactform.php on line 43
  • [22-Jan-2020 20:28:14 UTC] PHP Notice: Undefined index: autoreply in
    /home/e5z2aaq161fd/public_html/contactus/files/contactAssets/contactform.php on line 60
  • [22-Jan-2020 20:28:14 UTC] PHP Notice: Undefined index:
    autoreplysig in
    /home/e5z2aaq161fd/public_html/contactus/files/contactAssets/contactform.php on line 62
  • [22-Jan-2020 20:28:14 UTC] PHP Notice: Undefined index:
    autosubject in
    /home/e5z2aaq161fd/public_html/contactus/files/contactAssets/contactform.php on line 63
  • The errors are obvious. You're trying to access an index that doesn't exist. You can't just blindly assume that every time a request is made to your script that `$_POST` will necessarily contain the indexes you're expect. Use `isset()` to check that they exist first, before you attempt to use them. – Sherif Jan 24 '20 at 02:59
  • thanks for the input. I am really new to php, Will check it out! – bitsper2nd Jan 24 '20 at 03:30
  • You may also try `!empty()` – Tomas Gonzalez Jan 24 '20 at 04:20
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – 04FS Jan 24 '20 at 09:13
  • Yes. Another user already gave me good tips. Your link still is appreciated since it goes more in depth about resolving unidentified index. – bitsper2nd Jan 26 '20 at 02:15

1 Answers1

0

The php script is being executed without the expected POST variables being present in the request body. (This commonly happens when it is not a user submitting your form, but rather a spider or robot trying to find and execute your php files)

To prevent the errors from accumulating in you error log, You need to check if those $_POST values are set before you assign them. You can do this at least a couple ways:

  1. Check that they exist before running the entire script:

    if (isset($_POST['stackid']) && isset($_POST['emaillabel']) && isset($_POST['namelabel']) && isset($_POST['subjectlabel']) && isset($_POST['messagelabel']) && isset($_POST['mail'])  && isset($_POST['name']) && isset($_POST['subject']) && isset($_POST['text'])) {
    
        // all your php code here
    
    }
    
  2. Or you could check that the $_POST values are set before defining each variable, and if not, set it as an empty string:

    $emaillabel = isset($_POST['emaillabel']) ? strip_tags($_POST['emaillabel']) : '';
    $namelabel = isset($_POST['namelabel']) ? strip_tags($_POST['namelabel']) : '';
    $subjectlabel = isset($_POST['subjectlabel']) ? trip_tags($_POST['subjectlabel']) : '';
    $messagelabel = isset($_POST['messagelabel']) ? strip_tags($_POST['messagelabel']) : '';
    // ...etc...
    
  3. Or you could do a combination of both. First check for only the absolute required $_POST values you need to run the script and put them in the top if statement, and then when declaring the other variables, just check that the $_POST value is set first, and if not, set the var to an empty string.

Bryan Elliott
  • 4,055
  • 2
  • 21
  • 22
  • Thanks for the detailed answer. I edited the code in question to include the other lines shown in the error log. I decided to follow your logic and try the second option on my copy of the code. Will update later to see any changes. – bitsper2nd Jan 24 '20 at 04:28