redirect not working

When submitting a form, I am getting an error. The form itself along with the file is sending to my email just fine. But the “redirect” to the “thank you” page isn’t getting there. I will post my code in its entirety and fair warning the few php files are just over 1,000 lines of code. This is someone elses code I modified a few lines to fit my needs. So below I am posting the error, then an abbreviated version of the code, then the full code. (edit - my code has exceded the 20,000 character limit for this forum. I am posting a partial code up to line 192 for the fgcontactform.php. The other approx. 400 lines of code will be posted in message #2 listed below the original post).

My error code when hitting the submit button.
Warning: Cannot modify header information - headers already sent by (output started at /home/southpaw/public_html/foxenews.com/test/protected/privateheader.php:7) in /home/southpaw/public_html/foxenews.com/test/protected/include/fgcontactform.php on line 148

Line 7 in “privateheader.php”
[php][/php]

Line 148 in code “fgcontactform.php”
[php] header(“Location: $url”);[/php]

privateheader.php in full
[php]

Members only section

Tel: 970-462-7667 | Mail: [email protected]


Private Page

You are viewing a private page for members only



[/php]

[php]<?PHP
/*
Contact Form from HTML Form Guide

This program is free software published under the
terms of the GNU Lesser General Public License.

This program is distributed in the hope that it will
be useful - WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

@copyright html-form-guide.com 2010
*/
require_once(“class.phpmailer.php”);

/*
Interface to Captcha handler
/
class FG_CaptchaHandler
{
function Validate() { return false;}
function GetError(){ return ‘’;}
}
/

FGContactForm is a general purpose contact form class
It supports Captcha, HTML Emails, sending emails
conditionally, File atachments and more.
*/
class FGContactForm
{
var $receipients;
var $errors;
var $error_message;
var $name;
var $email;
var $date;
var $bigtitle;
var $shorttitle;
var $summery;
var $category;
var $message;
var $from_address;
var $form_random_key;
var $conditional_field;
var $arr_conditional_receipients;
var $fileupload_fields;
var $captcha_handler;

var $mailer;

function FGContactForm()
{
    $this->receipients = array();
    $this->errors = array();
    $this->form_random_key = 'HTgsjhartag';
    $this->conditional_field='';
    $this->arr_conditional_receipients=array();
    $this->fileupload_fields=array();

    $this->mailer = new PHPMailer();
    $this->mailer->CharSet = 'utf-8';
}

function EnableCaptcha($captcha_handler)
{
    $this->captcha_handler = $captcha_handler;
    session_start();
}

function AddRecipient($email,$name="")
{
    $this->mailer->AddAddress($email,$name);
}

function SetFromAddress($from)
{
    $this->from_address = $from;
}
function SetFormRandomKey($key)
{
    $this->form_random_key = $key;
}
function GetSpamTrapInputName()
{
    return 'sp'.md5('KHGdnbvsgst'.$this->GetKey());
}
function SafeDisplay($value_name)
{
    if(empty($_POST[$value_name]))
    {
        return'';
    }
    return htmlentities($_POST[$value_name]);
}
function GetFormIDInputName()
{
    $rand = md5('TygshRt'.$this->GetKey());

    $rand = substr($rand,0,20);
    return 'id'.$rand;
}


function GetFormIDInputValue()
{
    return md5('jhgahTsajhg'.$this->GetKey());
}

function SetConditionalField($field)
{
    $this->conditional_field = $field;
}
function AddConditionalReceipent($value,$email)
{
    $this->arr_conditional_receipients[$value] =  $email;
}

function AddFileUploadField($file_field_name,$accepted_types,$max_size)
{

    $this->fileupload_fields[] =
        array("name"=>$file_field_name,
        "file_types"=>$accepted_types,
        "maxsize"=>$max_size);
}

function ProcessForm()
{
    if(!isset($_POST['submitted']))
    {
       return false;
    }
    if(!$this->Validate())
    {
        $this->error_message = implode('<br/>',$this->errors);
        return false;
    }
    $this->CollectData();

    $ret = $this->SendFormSubmission();

    return $ret;
}

function RedirectToURL($url)
{
    header("Location: $url");
    exit;
}

function GetErrorMessage()
{
    return $this->error_message;
}
function GetSelfScript()
{
    return htmlentities($_SERVER['PHP_SELF']);
}

function GetName()
{
    return $this->name;
}
function GetEmail()
{
    return $this->email;
}
function GetDate()
{
    return $this->date;
}
function GetBigtitle()
{
    return $this->bigtitle;
}
function GetShorttitle()
{
    return $this->shorttitle;
}
function GetSummery()
{
    return $this->summery;
}
function GetCategory()
{
    return $this->category;
}
function GetMessage()
{
    return htmlentities($this->message,ENT_QUOTES,"UTF-8");
}

[/php]

contactform.php (THIS IS THE FORM THE USER SEES TO FILL OUT)
[php]<?php include("privateheader.php"); ?>

<?PHP /* Contact Form from HTML Form Guide This program is free software published under the terms of the GNU Lesser General Public License. See this page for more info: http://www.html-form-guide.com/contact-form/contact-form-attachment.html */ require_once("./include/fgcontactform.php"); $formproc = new FGContactForm(); //1. Add your email address here. //You can add more than one receipients. $formproc->AddRecipient('[email protected]'); //<SetFormRandomKey('027Lb15iBD8oBS7'); $formproc->AddFileUploadField('photo','jpg,jpeg,gif,png,bmp,doc,docx,zip,txt',102400); if(isset($_POST['submitted'])) { if($formproc->ProcessForm()) { $formproc->RedirectToURL("thank-you.php"); } } ?> Contact us

Article Submission Form


Use this form to submit your article. Please follow the guidelines at the very bottom of the page below the form.

* = Required Fields (IF YOU GET AN ERROR IT WILL SHOW UP DIRECTLY BELOW THIS TEXT)
<?php echo $formproc->GetErrorMessage(); ?>

Your Full Name*: ' maxlength="150" />
Email Address*: ' maxlength="100" size="29" />
Date for Article*: ' maxlength="50" />Date you want to appear in your article.
Title*: ' maxlength="107" size="80" />Title for your article (107 characters max. including spaces).
Abbreviated Title*: ' maxlength="32" size="34" />If the title is above is shorter than 32 characters (including spaces) please copy it into this box too. This title is mandatory for archive purposes.
Article Summery*: <?php echo $formproc->SafeDisplay('summery') ?>314 characters maximum. This summery will go under the slideshow on the home page.
Category*: ' maxlength="250" size="62" />Category your article is going to be placed under. (Sports, Tech, News, Video, Entertainment or new category if you have more articles you would like to submit in a new category, please have multiple articles prepared so we can fill the page with more than one article.)
Your personal web page link: ' maxlength="250" size="62" />If you want to submit a link to your personal web page, feel free to do so. It will be placed at the bottom of your article as a link.
Article: <?php echo $formproc->SafeDisplay('message') ?>
Copy and paste your article in the large window above or browse to the file by using the "Choose File" button below.

Upload your file: (You may only select 1 file using this option). It is best if you put all your files in a zip folder and send them thru this web form. If you would like to upload multiple files, please do so at the Google Drive portion of the web site. Please let me know you have uploaded the file(s) to Google Drive. I do not get an automatic notification when a file is placed there. If you need the user name and password for Google Drive, email me and I can send it to you.
  • Submit 2 photos that have not been shrunk down to any size. If it is a lengthy article, submit a few more photos up to 5.
  • Rename the photos so they identify with the article and please keep the name in all lower case letters with no spaces,
    it makes it much easier for me to post. NUMBER YOUR PHOTOS IN THE ORDER YOU WANT THEM PUBLISHED.
    When I get 1cellphone.jpg, 2cellphone.jpg and 3cellphone.jpg, the photo 1cellphone.jpg is the photo you want closest
    to the top and/or on the front page. IF PHOTOS ARE NOT NUMBERED, I WILL SELECT A RANDOM ONE TO BE POSTED.
  • Please make sure spell checking and grammar is done prior to submitting. I do not check your articles before they are posted.
  • If you would like to include a small profile picture of your mug shot to go with your article, include it in your attachments when sending your article.
  • This form does not automatically post your article when you submit it. This post will be emailed to me and then I will manually put it up on the web page.
  • If this form gives you some sort of error, please email me the error that is coming up to [email protected]


  • Ignore small window above, I don't know
    how to get rid of it and if you
    input anything the form will not work. <?php include("privatefooter.php"); ?>

    [/php]

    thank-you.php (the page that is suppose to show up after the submission button is hit but isn’t)
    [php]

    Thank you!

    Thanks for contacting us!

    [/php]

    (part 2 of the code for the fgcontactform.php)

    [php]

    /*-------- Private (Internal) Functions -------- */

    function SendFormSubmission()
    {
        $this->CollectConditionalReceipients();
    
        $this->mailer->CharSet = 'utf-8';
        
        $this->mailer->Subject = "Article Submission from $this->name";
    
        $this->mailer->From = $this->GetFromAddress();
    
        $this->mailer->FromName = $this->name;
    
        $this->mailer->AddReplyTo($this->email);
    
        $message = $this->ComposeFormtoEmail();
    
        $textMsg = trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s','',$message)));
        $this->mailer->AltBody = @html_entity_decode($textMsg,ENT_QUOTES,"UTF-8");
        $this->mailer->MsgHTML($message);
    
        $this->AttachFiles();
    
        if(!$this->mailer->Send())
        {
            $this->add_error("Failed sending email!");
            return false;
        }
    
        return true;
    }
    
    function CollectConditionalReceipients()
    {
        if(count($this->arr_conditional_receipients)>0 &&
          !empty($this->conditional_field) &&
          !empty($_POST[$this->conditional_field]))
        {
            foreach($this->arr_conditional_receipients as $condn => $rec)
            {
                if(strcasecmp($condn,$_POST[$this->conditional_field])==0 &&
                !empty($rec))
                {
                    $this->AddRecipient($rec);
                }
            }
        }
    }
    
    /*
    Internal variables, that you donot want to appear in the email
    Add those variables in this array.
    */
    function IsInternalVariable($varname)
    {
        $arr_interanl_vars = array('scaptcha',
                            'submitted',
                            $this->GetSpamTrapInputName(),
                            $this->GetFormIDInputName()
                            );
        if(in_array($varname,$arr_interanl_vars))
        {
            return true;
        }
        return false;
    }
    
    function FormSubmissionToMail()
    {
        $ret_str='';
        foreach($_POST as $key=>$value)
        {
            if(!$this->IsInternalVariable($key))
            {
                $value = htmlentities($value,ENT_QUOTES,"UTF-8");
                $value = nl2br($value);
                $key = ucfirst($key);
                $ret_str .= "<div class='label'>$key :</div><div class='value'>$value </div>\n";
            }
        }
        foreach($this->fileupload_fields as $upload_field)
        {
            $field_name = $upload_field["name"];
            if(!$this->IsFileUploaded($field_name))
            {
                continue;
            }        
            
            $filename = basename($_FILES[$field_name]['name']);
    
            $ret_str .= "<div class='label'>File upload '$field_name' :</div><div class='value'>$filename </div>\n";
        }
        return $ret_str;
    }
    
    function ExtraInfoToMail()
    {
        $ret_str='';
    
        $ip = $_SERVER['REMOTE_ADDR'];
        $ret_str = "<div class='label'>IP address of the submitter:</div><div class='value'>$ip</div>\n";
    
        return $ret_str;
    }
    
    function GetMailStyle()
    {
        $retstr = "\n<style>".
        "body,.label,.value { font-family:Arial,Verdana; } ".
        ".label {font-weight:bold; margin-top:5px; font-size:1em; color:#333;} ".
        ".value {margin-bottom:15px;font-size:0.8em;padding-left:5px;} ".
        "</style>\n";
    
        return $retstr;
    }
    function GetHTMLHeaderPart()
    {
         $retstr = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">'."\n".
                   '<html><head><title></title>'.
                   '<meta http-equiv=Content-Type content="text/html; charset=utf-8">';
         $retstr .= $this->GetMailStyle();
         $retstr .= '</head><body>';
         return $retstr;
    }
    function GetHTMLFooterPart()
    {
        $retstr ='</body></html>';
        return $retstr ;
    }
    function ComposeFormtoEmail()
    {
        $header = $this->GetHTMLHeaderPart();
        $formsubmission = $this->FormSubmissionToMail();
        $extra_info = $this->ExtraInfoToMail();
        $footer = $this->GetHTMLFooterPart();
    
        $message = $header."Submission from 'Submit Article' form: \n<p>$formsubmission</p><hr/>$extra_info".$footer;
    
        return $message;
    }
    
    function AttachFiles()
    {
        foreach($this->fileupload_fields as $upld_field)
        {
            $field_name = $upld_field["name"];
            if(!$this->IsFileUploaded($field_name))
            {
                continue;
            }
            
            $filename =basename($_FILES[$field_name]['name']);
    
            $this->mailer->AddAttachment($_FILES[$field_name]["tmp_name"],$filename);
        }
    }
    
    function GetFromAddress()
    {
        if(!empty($this->from_address))
        {
            return $this->from_address;
        }
    
        $host = $_SERVER['SERVER_NAME'];
    
        $from ="nobody@$host";
        return $from;
    }
    
    function Validate()
    {
        $ret = true;
        //security validations
        if(empty($_POST[$this->GetFormIDInputName()]) ||
          $_POST[$this->GetFormIDInputName()] != $this->GetFormIDInputValue() )
        {
            //The proper error is not given intentionally
            $this->add_error("Automated submission prevention: case 1 failed");
            $ret = false;
        }
    
        //This is a hidden input field. Humans won't fill this field.
        if(!empty($_POST[$this->GetSpamTrapInputName()]) )
        {
            //The proper error is not given intentionally
            $this->add_error("Automated submission prevention: case 2 failed");
            $ret = false;
        }
    
        //name validations
        if(empty($_POST['name']))
        {
            $this->add_error("Please provide your name.");
            $ret = false;
        }
        else
        if(strlen($_POST['name'])>150)
        {
            $this->add_error("Name is too big!");
            $ret = false;
        }
    
        //email validations
        if(empty($_POST['email']))
        {
            $this->add_error("Please provide your email address.");
            $ret = false;
        }
        else
        if(strlen($_POST['email'])>150)
        {
            $this->add_error("Email address is too big!");
            $ret = false;
        }
        else
        if(!$this->validate_email($_POST['email']))
        {
            $this->add_error("Please provide a valid email address");
            $ret = false;
        }
    	//date validations
        if(empty($_POST['date']))
        {
            $this->add_error("Please provide a date to post on your article. You have 3 options: 1) Use the date the event happened, 2) Use the date you wrote the article, 3) Use the date you are submitting this article.");
            $ret = false;
        }
        else
        if(strlen($_POST['date'])>50)
        {
            $this->add_error("Date is too big!");
            $ret = false;
        }
        //bigtitle validations
        if(empty($_POST['bigtitle']))
        {
            $this->add_error("Please provide a title. Maximum of 107 characters (including spaces).");
            $ret = false;
        }
        else
        if(strlen($_POST['bigtitle'])>108)
        {
            $this->add_error("Title is too big. Maximum of 107 characters please.");
            $ret = false;
        }
        //short title validations
        if(empty($_POST['shorttitle']))
        {
            $this->add_error("Please provide a short title. If the title is above is shorter than 32 characters (including spaces) please copy it into this box too. This title is mandatory for archive purposes.");
            $ret = false;
        }
        else
        if(strlen($_POST['shorttitle'])>33)
        {
            $this->add_error("Title is too big. Maximum of 32 characters (including spaces).");
            $ret = false;
        }
        //summery validations
        if(empty($_POST['summery']))
        {
            $this->add_error("Please provide a summery. This will be placed on the main page under the large slideshow image. Maximum length of 314 characters (including spaces). Go to the home page to view example.");
            $ret = false;
        }
        else
        if(strlen($_POST['summery'])>315)
        {
            $this->add_error("Summery is too big. Shorten length to 314 characters please. ");
            $ret = false;
        }
        //category validations
        if(empty($_POST['category']))
        {
            $this->add_error("Please provide a category. If you have an idea for a new category, please have multiple articles prepared so we can fill the page with more than one article.");
            $ret = false;
        }
        else
        if(strlen($_POST['category'])>251)
        {
            $this->add_error("Category is too big. Shorten length to 250 characters please. ");
            $ret = false;
        }
    
    
    
    
        //message validaions
        if(strlen($_POST['message'])>104800)
        {
            $this->add_error("Message is too big!");
            $ret = false;
        }
    
        //captcha validaions
        if(isset($this->captcha_handler))
        {
            if(!$this->captcha_handler->Validate())
            {
                $this->add_error($this->captcha_handler->GetError());
                $ret = false;
            }
        }
        //file upload validations
        if(!empty($this->fileupload_fields))
        {
         if(!$this->ValidateFileUploads())
         {
            $ret = false;
         }
        }
        return $ret;
    }
    
    function ValidateFileType($field_name,$valid_filetypes)
    {
        $ret=true;
        $info = pathinfo($_FILES[$field_name]['name']);
        $extn = $info['extension'];
        $extn = strtolower($extn);
    
        $arr_valid_filetypes= explode(',',$valid_filetypes);
        if(!in_array($extn,$arr_valid_filetypes))
        {
            $this->add_error("Valid file types are: $valid_filetypes");
            $ret=false;
        }
        return $ret;
    }
    
    function ValidateFileSize($field_name,$max_size)
    {
        $size_of_uploaded_file =
                $_FILES[$field_name]["size"]/104800;//size in KBs
        if($size_of_uploaded_file > $max_size)
        {
            $this->add_error("The file is too big. File size should be less than $max_size KB");
            return false;
        }
        return true;
    }
    
    function IsFileUploaded($field_name)
    {
        if(empty($_FILES[$field_name]['name']))
        {
            return false;
        }
        if(!is_uploaded_file($_FILES[$field_name]['tmp_name']))
        {
            return false;
        }
        return true;
    }
    function ValidateFileUploads()
    {
        $ret=true;
        foreach($this->fileupload_fields as $upld_field)
        {
            $field_name = $upld_field["name"];
    
            $valid_filetypes = $upld_field["file_types"];
            
            if(!$this->IsFileUploaded($field_name))
            {
                continue;
            }
    
            if($_FILES[$field_name]["error"] != 0)
            {
                $this->add_error("Error in file upload; Error code:".$_FILES[$field_name]["error"]);
                $ret=false;
            }
    
            if(!empty($valid_filetypes) &&
             !$this->ValidateFileType($field_name,$valid_filetypes))
            {
                $ret=false;
            }
    
            if(!empty($upld_field["maxsize"]) &&
            $upld_field["maxsize"]>0)
            {
                if(!$this->ValidateFileSize($field_name,$upld_field["maxsize"]))
                {
                    $ret=false;
                }
            }
    
        }
        return $ret;
    }
    
    function StripSlashes($str)
    {
        if(get_magic_quotes_gpc())
        {
            $str = stripslashes($str);
        }
        return $str;
    }
    /*
    Sanitize() function removes any potential threat from the
    data submitted. Prevents email injections or any other hacker attempts.
    if $remove_nl is true, newline chracters are removed from the input.
    */
    function Sanitize($str,$remove_nl=true)
    {
        $str = $this->StripSlashes($str);
    
        if($remove_nl)
        {
            $injections = array('/(\n+)/i',
                '/(\r+)/i',
                '/(\t+)/i',
                '/(%0A+)/i',
                '/(%0D+)/i',
                '/(%08+)/i',
                '/(%09+)/i'
                );
            $str = preg_replace($injections,'',$str);
        }
    
        return $str;
    }
    
    /*Collects clean data from the $_POST array and keeps in internal variables.*/
    function CollectData()
    {
        $this->name = $this->Sanitize($_POST['name']);
        $this->email = $this->Sanitize($_POST['email']);
    
        /*newline is OK in the message.*/
        $this->message = $this->StripSlashes($_POST['message']);
    }
    
    function add_error($error)
    {
        array_push($this->errors,$error);
    }
    function validate_email($email)
    {
        return eregi("^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$", $email);
    }
    
    function GetKey()
    {
        return $this->form_random_key.$_SERVER['SERVER_NAME'].$_SERVER['REMOTE_ADDR'];
    }
    

    }

    ?>
    [/php]

    Sponsor our Newsletter | Privacy Policy | Terms of Service