-1

I have an html form that posts to post_form.php for processing. I am getting this error when I attempt to submit the contact form:

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at C:\wamp\www\advanced-dynamic-php-form\form-files\post_form.php:1) in C:\wamp\www\advanced-dynamic-php-form\form-files\post_form.php on line 2
Call Stack
#   Time    Memory  Function    Location
1   0.0011  283424  {main}( )   ..\post_form.php:0
2   0.0011  284240  session_start ( )   ..\post_form.php:2

I have checked to be sure that session_start(); appears first on the page after the opening php tag. I have also checked my php.ini file to be sure that session.auto_start = 0. Here's the php page:

<?php
session_start();
isset($_FILES['contact_form']);
$body = "";
if(isset($_POST["submit"])){

    if(isset($_POST["captcha"])&&$_POST["captcha"]!=""&&$_SESSION["code"]==$_POST["captcha"])
    {

        $to = "info@lexomatic.com";
        $subject = "Contact";

        $name = $_POST['name'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $address = $_POST['address'];
        $message = $_POST['message'];
        $total_lines = $_POST['total_lines'];



            $dirname = str_replace ("@","",$email);
            $allowed =  array('jpg', 'png', 'gif', 'doc', 'docx', 'pdf', 'xls', 'xlsx', 'zip', 'rar', 'JPG', 'PNG', 'GIF', 'DOC', 'DOCX', 'PDF', 'XLS', 'XLSX', 'ZIP', 'RAR');

            $i=0;
            $num_of_att = 0;
            $error_ext = 0;
            $error_size = 0;
            $upload_error =0;
            $target1 = "";
            $target2 = "";
            $target3 = "";

            $img_url1 = "";
            $img_url2 = "";
            $img_url3 = "";

            // File Attachment Function
            for ($file_num=1; $file_num<=3; $file_num++){
                ${'ok'.$file_num} = 0;
                $input_name = "file".$file_num;

                if(!empty($_FILES[$input_name]["name"] )){
                    ${'target'.$file_num} = $_SERVER['DOCUMENT_ROOT'] . '/form-files/uploads/'.$dirname .'/'. $_FILES[$input_name]["name"];
                    ${'img_url'.$file_num} = 'http://YOURSITEURL.COM/form-files/uploads/'.$dirname .'/'. $_FILES[$input_name]["name"];
                    $file_ext = $_FILES[$input_name]["name"];
                    $ext = end(explode(".", $file_ext));
                    //$file_ext = new SplFileInfo(${'target'.$file_num});
                    //$ext  = $file_ext->getExtension();


                    if(!in_array($ext,$allowed) ) {
                        $error_ext++;
                        $error_message = "<div style='width: 250px; color: red; font-family: Arial; font-weight: normal; font-size:12px'>Extensions " .$ext. " are not allowed</div>";
                    } elseif(($_FILES[$input_name]['size'] > 2097152) || ($_FILES[$input_name]['size'] == 0)){
                        $error_size++;
                        $error_message = "<div style='margin-left:15px; width: 250px; color: red; font-family: Arial; font-weight: normal; font-size:12px'>File " .$input_name. " exceeds max file size</div>";
                    }else{
                        ${'ok'.$file_num} = 1;
                        $num_of_att++;
                        ${'cur_temp_name'.$file_num} = $_FILES[$input_name]["tmp_name"];                        
                    } //close if
                }//close if
            }//close for

            // File attachment check
            $counter = 0;
            if(($num_of_att>0) && ($error_size==0) && ($error_ext==0)){ 
                if (!file_exists("uploads/".$dirname)) {
                    mkdir("uploads/$dirname", 0777, true);
                }

                if($ok1 == 1){
                    if(move_uploaded_file($cur_temp_name1, $target1)){
                        $counter++;
                    }else{
                        $upload_error++;
                    }
                }
                if($ok2 == 1){
                    if(move_uploaded_file($cur_temp_name2, $target2)){
                        $counter++;
                    }else{
                        $upload_error++;
                    }
                }
                if($ok3 == 1){
                    if(move_uploaded_file($cur_temp_name3, $target3)){
                        $counter++;
                    }else{
                        $upload_error++;
                    }
                }

            }   
                //////////////////////// Create Email Body ////////////////////////////////


        //  if(($num_of_att==0) && ($error_size==0) && ($error_ext==0)){
                    /*while (list($key,$value) = each($_POST)){
                        $body .= $key.": " .$value."\n";
                    }*/
            if(($upload_error == 0) && ($error_size==0) && ($error_ext==0)){    
                $body .= "Name: ".$name."\n";
                $body .= "Email: ".$email."\n";
                $body .= "Phone: ".$phone   ."\n";
                $body .= "Address: ".$address."\n";
                $body .= "Message: ".$message."\n\n";


                for($i=1; $i<=20; $i++){

                    $dynamicentryvaluefirst = 'dynamicentryvaluefirst'.$i;
                    $dynamicentryvaluesecond = 'dynamicentryvaluesecond'.$i;
                    $dynamicentryvaluethird = 'dynamicentryvaluethird'.$i;
                    $dynamicentryvaluefourth = 'dynamicentryvaluefourth'.$i;
                    $dynamicentryvaluefifth = 'dynamicentryvaluefifth'.$i;

                    if (isset($_POST[$dynamicentryvaluefirst])){
                        $cur_dynamicentryvaluefirst = $_POST[$dynamicentryvaluefirst];
                    }else{
                        $cur_dynamicentryvaluefirst ="";
                    }

                    if (isset($_POST[$dynamicentryvaluesecond])){
                        $cur_dynamicentryvaluesecond = $_POST[$dynamicentryvaluesecond];
                    }else{
                        $cur_dynamicentryvaluesecond ="";
                    }

                    if (isset($_POST[$dynamicentryvaluethird])){
                        $cur_dynamicentryvaluethird = $_POST[$dynamicentryvaluethird];
                    }else{
                        $cur_dynamicentryvaluethird ="";
                    }

                    if (isset($_POST[$dynamicentryvaluefourth])){
                        $cur_dynamicentryvaluefourth = $_POST[$dynamicentryvaluefourth];
                    }else{
                        $cur_dynamicentryvaluefourth ="";
                    }

                    if (isset($_POST[$dynamicentryvaluefifth])){
                        $cur_dynamicentryvaluefifth = $_POST[$dynamicentryvaluefifth];
                    }else{
                        $cur_dynamicentryvaluefifth ="";
                    }


                    ${'line'.$i} = $cur_dynamicentryvaluefirst . " | " . $cur_dynamicentryvaluesecond . " | " . $cur_dynamicentryvaluethird . " | " . $cur_dynamicentryvaluefourth . " | " . $cur_dynamicentryvaluefifth;
                    if(${'line'.$i} == (" |  |  |  | ")){

                    }else{
                        $body .= "Line".$i.": ".(${'line'.$i})."\n";
                    }
                }

                $body .= "\n"."File 1: ".$img_url1."\n";
                $body .= "File 2: ".$img_url2."\n";
                $body .= "File 3: ".$img_url3."\n";

                $sent = mail($to, $subject, $body); 
                //echo "Body----------------------"."<br/>".$body;

                echo "<div style='position: relative; top: -10px; margin-left:15px; width: 250px; color: green; font-family: Arial; font-weight: normal; font-size:12px'><img src='tick-icon.png' width='70px' /><div id='message_result'>The form submitted successfully.</div></div>";

            }else{

                echo "<div style='margin-left:15px; width: 250px; color: red; font-family: Arial; font-weight: normal; font-size:12px'><img src='delete-icon.png' width='70px' /><div id='message_result'>An error occured while uploading the file(s).<br/>". $error_message . "</div></div>";
            }   
        }else{
            echo "<div style='margin-left:15px; width: 250px; color: red; font-family: Arial; font-weight: normal; font-size:12px'><img src='delete-icon.png' width='70px' /><div id='message_result'>Wrong security code number.</div></div>"; // invalid code
        }   
}else{
    echo "<div style='margin-left:15px; width: 250px; color: red; font-family: Arial; font-weight: normal; font-size:12px'><img src='delete-icon.png' width='70px' /><div id='message_result'>An error occured. <br/>Please check file sizes and types & try again.</div></div>";
    session_destroy();
}


?>

Any assistance would be appreciated.

lexomatic
  • 13
  • 4
  • No output of any kind, including whitespace/linebreaks is permitted before` – Michael Berkowski Apr 24 '14 at 13:19
  • Thank you for the quick response. The entire file is included with the question. There isn't any whitespace and it is not UTF-8 encoded. Any other suggestions? – lexomatic Apr 24 '14 at 13:20
  • Please don't code-dump your entire program in a question, try to pare it down to just the relevant parts. – GordonM Apr 24 '14 at 13:21
  • Sorry, GordonM, but I didn't know if anything else in the file could be causing the error. – lexomatic Apr 24 '14 at 13:23
  • Reading the error message can help with that lexomatic; only the first two lines are relevant. Know that error messages don't lie :) and the problem you've got is very common. – AD7six Apr 24 '14 at 18:45

1 Answers1

0

assumed everything between <?php ... and ... ?> would be deleted, then the remaining rest will be "outputed" to the browser as html.

This is always a p-i-t-a as newlines, whitespaces, invisable chars like backspace etc before and after the php tags and even in between them will possibly mess up your ouput.

things like

<?php
      /**
       * some comments
       */
?>

<?php
...

will take you hours to find. Here: the two (!) nl's between ?> and the second <?php

Axel Amthor
  • 10,980
  • 1
  • 25
  • 44