PHP标题不重定向 [英] PHP Header not redirecting

查看:129
本文介绍了PHP标题不重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这段代码由一个表单提交。

  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 - ] +)*(\。[az] {2,3})$,$ email)!= FALSE){
$ conn = connect();
$ sql =选择ID从订户WHERE电子邮件=?;
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订户(名称,电子邮件,密码)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);



$ div $解析方案

标题的PHP文档:


请记住,必须在发送任何实际输出之前调用header(),或者使用普通的HTML标记,文件中的空行或PHP。使用include()或require()函数或其他文件访问函数读取代码,并在调用header()之前输出空格或空行是非常常见的错误。使用单个PHP / HTML文件时存在同样的问题


您确定在调用 header


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

解决方案

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?

这篇关于PHP标题不重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆