-4

I have a simple registration form set up, on sending the details to my function to put the data in the database I want the page to redirect to a success page...

This works brilliantly on my local server but when I uploaded it to my live server its just not redirecting

The redirect that is not working is on line 65 :) it just redirects to register.php?success

Any help would be gratefully received. I've seen a few people have had the same problem but their solution would not work for me :(

ALL other header locations work. just this one won't :@

    <?php
    include 'core/init.php';
    //check if logged in
    logged_in_redirect();
    include 'includes/overall/header.php'; 

if (empty($_POST) === FALSE) {
    $required_fields = array('username', 'password', 'password_again', 'first_name', 'last_name', 'email');
    foreach ($_POST as $key => $value) {
        if (empty($value) && in_array($key, $required_fields) === true) {
            $errors[] = 'You appear to have missed something out, all fields are required.';
            break 1;

        }
    }

    if (empty($errors) === true) {
        if (user_exists($_POST['username']) === true ) {
            $errors[] = 'Sorry, the username \'' . $_POST['username'] . '\' is already taken.';
        }
        if (strlen($_POST['username']) < 6) {
            $errors[] = 'Sorry, your username must be at least 6 characters.';
        }
        if (strlen($_POST['username']) > 25) {
            $errors[] = 'Sorry, your username must be under 25 characters.';
        }
        if (preg_match("/\\s/", $_POST['username']) == true) {
            $errors[] = 'Your username must not contain any spaces.';
        }
        if (strlen($_POST['password']) < 6) {
            $errors[] = 'Sorry, your password must be at least 6 characters.';
        }
        if ($_POST['password'] !== $_POST['password_again']) {
            $errors[] = 'Sorry, your passwords do not match.';
        }
        if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false ) {
            $errors[] = 'Sorry, you did not provide a valid email address.';
        }
        if (email_exists($_POST['email']) === true ) {
            $errors[] = 'Sorry, the email \'' . $_POST['email'] . '\' is already registered to an account.';
        }
    }
}

?>
<h1>Register</h1>

<?php 
if (isset($_GET['success']) && empty($_GET['success'])) {
    echo "You have been registered successfully.";
} else {
    if (empty($_POST) === false && empty($errors) === true) {
        // register user
        $register_data = array(
            'username'      => $_POST['username'],
            'password'      => $_POST['password'],
            'first_name'    => $_POST['first_name'],
            'last_name'     => $_POST['last_name'],
            'email'         => $_POST['email'],
            'email_code'    => md5($_POST['username'] + microtime())
        );

        register_user($register_data);
        //header location not working *********************
        header('Location: register.php?success');
        exit();

    } else if (empty($errors) === false) {
        //error output
        echo output_errors($errors);
    }

?>
    <form action="" method="post">
        Register your account here, all fields are required.
        <ul>
            <li>
                Username: </br>
                <input type="text" name="username"/>
            </li>
            <li>
                Password: </br>
                <input type="password" name="password"/>
            </li>
            <li>
                Repeat Password: </br>
                <input type="password" name="password_again"/>
            </li>
            <li>
                First Name: </br>
                <input type="text" name="first_name"/>
            </li>
            <li>
                Last Name: </br>
                <input type="text" name="last_name"/>
            </li>
            <li>
                Email: </br>
                <input type="text" name="email"/>
            </li>
            <li>
                <input type="submit" value="Register"/>
            </li>
        </ul>
    </form>

<?php 
}
include 'includes/overall/footer.php'; 


?>
Ste Hughes
  • 19
  • 1
  • 5
  • 5
    I see whitespace before the opening `Register` output. No output before `header()` calls! – Michael Berkowski Jul 17 '13 at 01:37
  • Do you see any errors in your server error log? –  Jul 17 '13 at 01:37
  • Show the response headers. – zerkms Jul 17 '13 at 01:37
  • 1
    this code cannot work in any location. `

    Register

    ` sends headers, so any further `header()` call will fail.
    – Sebas Jul 17 '13 at 01:38
  • 1
    If you turn on output buffering, it'll work, but if not, as Sebas has pointed out, you'll need to send the header before sending any other output. – Chelsea Urquhart Jul 17 '13 at 01:40
  • Should fix it: http://pastie.org/8147517 – Amal Murali Jul 17 '13 at 01:41
  • Output buffering is the key here - depending on your php.ini settings, you might have, for instance the `obgzhandler` enabled (transparently compress all responses). Since this doesn't start outputting until enough output has gone through the buffer for the compression algorithm to properly kick in, `header()` calls will still succeed. A slightly different setup on a different server (or a little bit more output) and you will see the error. – IMSoP Jul 17 '13 at 01:51
  • Output buffering is a hack fr not structuring your code properly –  Jul 17 '13 at 02:03

2 Answers2

0

Most likely, output buffers are on in the development environment and off in the live environment. Also, displaying errors to users must be off in the live environment or the exact error (output started before headers) would have shown up in the browser.

Check you ini file for this stuff: http://php.net/manual/en/outcontrol.configuration.php

Vivek
  • 101
  • 1
  • 1
0

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

loncool
  • 11
  • 1
  • thanks! :) This is my first big project, I've learnt alot so far :) I fixed the problem by only including the header when I need it, bit messy but its working :D I'll tidy it up before I go live. – Ste Hughes Jul 17 '13 at 13:48