header()无法重定向

时间:2013-02-20 12:16:57

标签: php redirect header mysqli

我不知道为什么我的代码无法重定向...我做的错误... 它显示出一个警告 (((警告:无法修改标题信息 - 已经发送的标题)))...请任何人告诉我需要更改为成功重定向...我的代码是::

<?php
include_once $_SERVER['DOCUMENT_ROOT'].'/include/db.inc.php' ;
$email = $_POST['email'];
$password1 = $_POST['password1'];
$password2 = $_POST['password2'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
if($password1 != $password2)
{
    header('Location: http://localhost/home/');
    echo "<script language=javascript> alert(\"RE-ENTERED PASSWORD DOESN'T MATCH           WITH THE ORIGINAL ONE\");</script>"; 
    exit();
}
if($firstname == "" or $firstname == "First Name")
{
    header('Location: http://localhost/home/');
    echo "<script language=javascript> alert(\"INVALID FIRST NAME\");</script>"; 
    exit();
}
if($lastname == "" or $lastname == "Last Name")
{
    header('Location: http://localhost/home/');
    echo "<script language=javascript> alert(\"INVALID LAST NAME\");</script>"; 
    exit();
 }
//INSERTION INTO THE DATABASE STARTS FROM HERE.........
$email = mysqli_real_escape_string($link,$_POST['email']);
$password1= mysqli_real_escape_string($link,$_POST['password1']); 
$firstname= mysqli_real_escape_string($link,$_POST['firstname']);
$lastname= mysqli_real_escape_string($link,$_POST['lastname']);
$password=md5($password1);
$sql = "INSERT INTO users SET 
    email = '$email',
    password = '$password',
    firstname = '$firstname',
    lastname = '$lastname'";
if (!mysqli_query($link, $sql))
{
    header('Location: http://localhost/home/');
    echo "<script language=javascript> alert(\"THIS E-MAIL ID HAS ALREADY USED BEFORE !!!\");</script>"; 
    exit();
}
else
{
    header('Location: http://localhost/home/');
    echo "<script language=javascript> alert(\"REGISTRATION SUCCESSFULLY COMPLETED !!!             \");</script>"; 
    exit();
}        
?>

2 个答案:

答案 0 :(得分:2)

检查db.inc.php中的任何输出(回声)或空格。

在致电header()

之前,您不得输出任何内容

此外,将您的文件编码为UTF-8 without BOM

答案 1 :(得分:0)

如果您无法找到空白区域或脚本输出的位置,请将此行放在脚本的顶部:

ob_start()

然后,在任何exit();

之前也需要此功能
ob_end_flush();