0

I am having some problems with redirecting to another page in Php.

I am using :

header("Location: mypage.html");

But instead of redirecting to this page it is just printing the html file of the 'mypage.html' into the console. With console being the debug console in the Google Chrome inspect view aka (ctrl + shift + i).

This is the relevant part of the code for this error:

<?php

    ob_clean(); // did not work...

    function isValid(){
        if(
            $_POST["name"] != "" &&
            $_POST["tel"] != "" &&
            $_POST["email"] != "" &&
            $_POST["msg"] != "" &&
            $_POST["reg"] != ""
        ) {
            return true;
        }
        return false;
    }

    if (isValid()){
        $recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify'; // URL to the reCAPTCHA server
        $recaptcha_secret = 'XXXXXXsecretkeyXXXXX'; // Secret key
        $recaptcha_response = $_POST['recaptchaResponse']; // Response from reCAPTCHA server, added to the form during processing
        $recaptcha = file_get_contents($recaptcha_url.'?secret='.$recaptcha_secret.'&response='.$recaptcha_response); // Send request to the server
        $recaptcha = json_decode($recaptcha); // Decode the JSON response

        if($recaptcha->success == true && $recaptcha->score >= 0.5 && $recaptcha->action == "submit"){
            // echo "Score : ".$recaptcha->score;
            // Send the email

            $Name = $_POST["name"];
            $Tel = $_POST["tel"];
            $MailFrom = $_POST["email"];
            $Message = UTF8_decode($_POST["msg"]);
            $Reg = $_POST["reg"];

            $MailTo  = "mail@mail.com";
            $Subject = "".$Name." | Mail@mail.com";

            $Headers = array(
                'From'         => $MailTo,
                'Reply-To'     => $MailTo,
                'MIME-Version' => '1.0',
                'Content-type' => 'text/html',
                'charset'      => 'UTF-8',
                'X-Mailer'     => 'PHP/' . phpversion()
            );

            $Msg = 'Namn: ' . $Name . '<br />Telefonnummer: ' . $Tel . '<br />E-mail: ' . $MailFrom . '<br />Regnummer: ' . $Reg . '<br /><br />Meddelande: ' . wordwrap($Message, 50);

            $test = mail($MailTo, $Subject, $Msg, $Headers);
            
            if ($test === true) {
                ob_clean(); // this did not work here either...
                header("Location: redirect.html"); // this is where the error is!
            } else {
                echo "Meddelandet kunde inte skickas, vänligen kontakta oss på följande mail. (mail@mail.com)";
            }

        } else {
            // reCAPTCHA failed, dont send mail
            echo "Vi tror att du är en robot, är du inte en robot så vänligen kontakta oss på följande mail. (mail@mail.com)";
        }
    }
?>

To fix this problem I have tried to remove all of the echos in the php file since I thought that it might not work with echos. But no success.

I have also tried to use a function called ob_clean() to prevent it from writing in the console but no luck there either.

Any ideas would be greatly appreciated.

  • 1
    I always use `exit();` after a `header`-redirect – Gowire Jan 02 '23 at 19:38
  • 1
    Can you tell how you access this PHP file? Seems like you access it via Ajax call? Since HTML content is printed out, means request ended up in your HTML page, just matter of how you show it. – Justinas Jan 02 '23 at 19:38
  • @Gowire In this case it's not needed. – Justinas Jan 02 '23 at 19:39
  • @Justinas, I use JS to access the PHP file, it looks something like this : fetch("sendmail.php", { method: "POST", body: new FormData(form), }) – Theo Esberg Jan 02 '23 at 19:40
  • @Justinas, Your answer got me thinking. It only prints the HTML content in the console and not on the site. But this made me think that it might be easier to just get a response from the PHP file and then in the JS that called the PHP file to use a .then() to change to the right page if the response is valid. Will try that now! Thanks getting me thinking on it from another angle! – Theo Esberg Jan 02 '23 at 19:49

0 Answers0