PHP header 不重定向

标签 php header

我的标题不会重定向。代码执行后它只是空白并且不执行重定向。文件中没有空格。除了重定向之外,代码完全正常工作。

此代码由表单提交调用。

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");
  }
}

最佳答案

根据 header 的 PHP 文档:

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

您确定在调用 header 之前没有向页面发送输出吗?

关于PHP header 不重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6396813/

相关文章:

php - 在客户端重用 Zend 验证器

php - 检查内容类型 POST 请求 PHP

python - 使用正则表达式解析 winerror.h 和 ntstatus.h 状态/定义

python - 如何在 Django 中集成 304?

Python pandas dataframe-从标题中删除列

php - 删除用户和唯一电子邮件地址

php - 使用 lightopenid/注销保留登录信息

PHP - mysqli_fetch_assoc,2 个结果然后放入数组中

javascript - 网页上的直播 channel 流

c - linux/kernel.h 中的 sysinfo 结构