我的标题不会重定向。代码执行后它只是空白并且不执行重定向。文件中没有空格。除了重定向之外,代码完全正常工作。
此代码由表单提交调用。
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/