1

I built a website locally with WAMP server, and everything worked fine, then ported over to web server on GoDaddy, and my redirect function suddenly stopped working.

Simple site at (www.minute.tech), you can test out the one form I have, goes to the form_processing.php, but should redirect back to the index page with a message. When you go back to the index page after the failed redirect, it still shows the proper message of "Booyah!...", and inputs the email into my database.

Any ideas why it won't redirect on my web server, but will on my local WAMP server with the same code? Cheers!

Here's my redirect function in sessions.php:

function redirect_to($new_location) {
header("Location: " . $new_location);
exit();
}

Here's process_email.php where I redirect:

<?php require_once("sessions.php"); ?>
<?php require_once("db_connection.php"); ?>
<?php require_once("functions.php"); ?>

    <?php 

    if(isset($_POST['submit']) && !empty($_POST['email'])){
        //Process form
        $email = $_POST['email'];
        $email = mysql_prep($email);
        $techcheck = (isset($_POST['techcheck'])) ? 1 : 0;

        // 2. Perform database query
        $query  = "INSERT INTO signups (";
        $query .= " email, techcheck";
        $query .= ") VALUES (";
        $query .= "'{$email}', $techcheck";
        $query .= ")";

        $result = mysqli_query($connection, $query);

        if ($result) {
            // Success
            $_SESSION["good_message"] = "Booyah! We will keep you posted on progress.";
            redirect_to("../index.php");
        } else {
            //Failure
            $_SESSION["bad_message"] = "Failed to accept email.";
            redirect_to("../index.php");
        }
    } else {
        //This can be an accidental GET request
        $_SESSION["bad_message"] = "That is not a valid email! Please try again.";
        redirect_to("../index.php");
    }

    ?>

3 Answers3

0

You have to start object for header function sometimes

Add this code to all pages

<?php ob_start(); ?>
ImBhavin95
  • 1,494
  • 2
  • 16
  • 29
  • 1
    "*Try this*" answers are not encouraged. Add an explanation of what you changed and why. That being said, output buffering isn't really needed when you can just fix the root of the issue instead. – Qirel Feb 27 '17 at 05:38
  • This seemed to do the trick, thank you so much! – douglasrcjames_old Feb 27 '17 at 09:02
0

Remove all the empty spaces. Some server set-ups will be okay with output before the header redirect but the vast majority of servers will not redirect properly. Turn on error reporting and it will probably tell you have output before the redirect:

<?php
// ^---- Just do one open tag
require_once("sessions.php"); // Remove close tag, possible empty space after
require_once("db_connection.php"); // Remove close tag, possible empty space after
require_once("functions.php"); // Remove close tag, possible empty space after
// Remove the close and open php tags
if(isset($_POST['submit']) && !empty($_POST['email'])){
        //Process form
        $email = $_POST['email'];
        $email = mysql_prep($email);
        $techcheck = (isset($_POST['techcheck'])) ? 1 : 0;

        // 2. Perform database query
        $query  = "INSERT INTO signups (";
        $query .= " email, techcheck";
        $query .= ") VALUES (";
        $query .= "'{$email}', $techcheck";
        $query .= ")";

        $result = mysqli_query($connection, $query);

        if ($result) {
            // Success
            $_SESSION["good_message"] = "Booyah! We will keep you posted on progress.";
            redirect_to("../index.php");
        } else {
            //Failure
            $_SESSION["bad_message"] = "Failed to accept email.";
            redirect_to("../index.php");
        }
    } else {
        //This can be an accidental GET request
        $_SESSION["bad_message"] = "That is not a valid email! Please try again.";
        redirect_to("../index.php");
    }
// If you have no more content below this point, just remove the close php tag
// it is not required and is a possible source of empty space down the line...

Also, you should not be using mysql_ anymore, it is deprecated and removed in PHP 7. Also, bind parameters instead of doing this parameter right into the sql:

$query .= "'{$email}', $techcheck";
Rasclatt
  • 12,498
  • 3
  • 25
  • 33
  • Wow, thank you so much for the tips! What do you mean by bind parameters instead of doing the parameter right into the sql? Also, mysql_prep() is a function that I built, are you saying I shouldnt use the underscore symbol? Istead the star symbol (*) after the underscore? – douglasrcjames_old Feb 27 '17 at 08:54
  • If you built the `mysql_prep()` you're fine then with ignoring a portion of my comment which is using `mysql_` because you are using just the `mysqli` functions which is good. But what I say about removing spaces and binding parameters still stands, you need to do those two things. Using the output buffer as suggested by the answer you marked as correct is a work-around. You haven't fixed the problem, only silenced it. As for bind_param(), it will be infinitely saver than any method you make to sanitize your variables into your sql. – Rasclatt Feb 27 '17 at 14:56
0

Do with JS.

<?php    
function redirect_to($new_location) { ?>

<script>window.location="<?php echo $new_location; ?>";</script>
<?php } ?>
SBimochan
  • 474
  • 4
  • 13