5

My header won't redirect. After the code is executed it's just blank and doesn't execute the redirect. There's no whitespace in the file. The code works completely correctly apart from the redirect.

This code is called by a form submit.

if(!empty($_POST['addSubscriber'])){
  $name = $_POST['name'];
  $email = $_POST['email'];
  if(!empty($name) && !empty($email) && eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email) != FALSE){
    $conn = connect();
    $sql = "SELECT id FROM subscribers WHERE email=?";
    if($stmt = $conn->prepare($sql)){
      $stmt->bind_param("s", $email);
      $stmt->execute();
      if($stmt->fetch()){
        header("Location: http://bcp350.org.uk/index.php?message=1");
      } else {
        $password = md5(uniqid());
        $sql2 = "INSERT INTO subscribers(name, email, password) VALUES(?, ?, '$password')";
        if($stmt2 = $conn->prepare($sql2)){
          $stmt2->bind_param("ss", $name, $email);
          $stmt2->execute();
          if($stmt2->affected_rows == 1)
            header("Location: http://bcp350.org.uk/index.php?message=1");
        }
      }
    }
  } else {
    header("Location: urlnotallowedbecauseofstackoverflowlimit");
  }
}
JAAulde
  • 19,250
  • 5
  • 52
  • 63
nbaosullivan
  • 4,034
  • 3
  • 21
  • 14
  • 5
    After which `header()` call does it fail? Also, **always** call `exit();` right after doing `header("Location:");` to stop further execution of the current script!` – Michael Berkowski Jun 18 '11 at 14:57
  • Is there something before the posted code in the *.php-file? – Christoph Fink Jun 18 '11 at 14:59
  • 2
    Put in the `exit()` calls after each of your your header redirects and see if it still fails. If it doesn't, then it's possible an error further down the page is still being executed and failing. – Michael Berkowski Jun 18 '11 at 14:59
  • By the code logic, redirection will be executed not in any case - there is 2 `if`s, when redirection will not be called. – OZ_ Jun 18 '11 at 15:02
  • The full php file that this code snippet is included in can be viewed here pastebin.com/Ha4LQu24 – nbaosullivan Jun 18 '11 at 15:02
  • I might add that if I put a echo here instead that that executes fine, it's just if it's a header. – nbaosullivan Jun 18 '11 at 15:03
  • It was whitespace in constants.php, thanks a lot though guys! – nbaosullivan Jun 18 '11 at 15:07
  • Possible duplicate of [PHP header location-redirect doesn't work - why?](http://stackoverflow.com/questions/2710079/php-header-location-redirect-doesnt-work-why) – Sruit A.Suk Dec 06 '15 at 22:28

3 Answers3

8

According to the PHP documentation for header:

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

Are you sure there is no output sent to the page prior to calling header?

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
3

If any of the following happens in your code, all calls to header() will be bypassed:

  1. $_POST['addSubscriber'] is empty
  2. $conn->prepare($sql) returns falsy
  3. $conn->prepare($sql2) returns falsy
  4. $stmt2->affected_rows does not compare to 1

You need to add some debugging and figure out which of those is happening.

JAAulde
  • 19,250
  • 5
  • 52
  • 63
-1

use this code in the very beginning of your php page

<?php

ob_start(); ?>