Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Cant get the thanks status

Right now im in Enhancing a simple php appliction -> Moving the other pages.

I cant get the status thanks after setting everything to BASE_URL. I have:

            header("Location:" . BASE_URL . "contact/?status=thanks");

I only get a blank page. To understand the problem i echoed out the URL that should be there. I get:

/shirts4mike_local/contact/?status=thanks

So why dont I get it in the url in my browser?

Logan R
Logan R
22,989 Points

What is the URL that you are redirected too?

Hi Denis,

Can you post the code for that page? If it's long then just post from the beginning until the header redirect.

3 Answers

One of the reasons a header redirect wouldn't work is because you might be sending output to the page before you get to the header redirect.

Output could be from an echo statement or even whitespace outside the php blocks.

ob_start() turns on output buffering so that any output you have is saved in an internal buffer instead of being sent out.

If this is fixing your problem then it could mean you do have some output to the page somewhere.

I don't see any echo statements.

Is your opening php tag at the beginning of the first line of your file?

If it is on the second line and line 1 is a blank line then the header redirect wouldn't work.

I would try to figure out where you have output something.

Thats the thing, the url does not change! it remains /contact/

Hey, thanks for the answer. Here ist he contact.php code:

<?php
require_once("../inc/config.php");

if ($_SERVER["REQUEST_METHOD"] == "POST"){
        // Trim = Leerzeichen vorne und hinten abschneiden
    $name = trim($_POST["name"]);
    $email = trim($_POST["email"]);
    $message = trim($_POST["message"]);


    if($name == "" OR $email == "" OR $message == "") {
    $error_message = "You must specify a value for your name, email adress and message.";

    }

    if (!isset($error_message)) {
    // check for malicious code
    foreach ($_POST as $value) {
        if(stripos($value, 'Content-Type') !== FALSE ) {
            $error_message = "There was a problem with the informatin you entered.";
        }
    }
    }   

    //Honeypot checkt ob ein Bot etwas in das unsichtbare(display:none) Formfeld schreibt.
    if(!isset($error_message) && $POST["address"] != "") {
        $error_message = "form submission error.";
    }

    //Email Funktionalität
    require_once(ROOT_PATH . "inc/phpmailer/class.phpmailer.php");
    $mail = new PHPMailer();
    //check if Email-adress is (!)not valid
    if(!isset($error_message) && !$mail -> ValidateAddress($email)) {
        $error_message = "You must specify a valid emailaddress";
    }
    //Überprüfe ob Fehlermeldung definiert ist
    if (!isset($error_message)) {
    //Email Funktionalität Ende
            $email_body = "";
            $email_body = $email_body."Name: ".$name."<br>";
            $email_body = $email_body."Email: ".$email."<br>";
            $email_body = $email_body."Message: ".$message."<br>";

        //  Send Email
            //
            //
            // Set PHPMailer to use the sendmail transport
        $mail->isSendmail();
        //Set who the message is to be sent from
        $mail->setFrom($email, $name);
        //Set who the message is to be sent to
        $mail->addAddress('Denis.mueller@peached.de', 'Denis');
        //Set the subject line
        $mail->Subject = 'Contact Form Submit from Shirts4Mike' . $name;
        //Read an HTML message body from an external file, convert referenced images to embedded,
        //convert HTML into a basic plain-text alternative body
        $mail->msgHTML($email_body);
        //send the message, check for errors
        if ($mail->send()) {
            //End sendmail
            //
            //
            header("Location:" . BASE_URL . "contact/?status=thanks");
            // Echo-Test
            //echo(BASE_URL . "contact/?status=thanks");
            exit;
        }
        else {
            $error_message = "There was a problem with sending the email: " . $mail->ErrorInfo;
        } 



        }

}
?>

I did everything as mentioned in the videos. Its only the redirect that is not working.

I got a working-solution by adding: ob_start(); at the beginning of the script.

It works, but I dont know what I am doing there....