0

i don't know why my code is failing to redirect..what mistake i have done... its showing an warning which is (((Warning: Cannot modify header information - headers already sent by))) ...please anybody tell what i need to change to redirect successfully... my code is ::

<?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();
}        
?>
RbG
  • 3,181
  • 3
  • 32
  • 43
  • 1
    Ensure there are no spaces in your include/db.inc.php. An ' ' (spaces / newlines after '?>') will produce this error. – tobspr Feb 20 '13 at 12:19
  • 1
    Redirect to a new URL **or** output HTML from the current URL. *Pick One*. – Quentin Feb 20 '13 at 12:20
  • @TobSpr..no there is no whitespace in db.inc.php..there must be some other problem..please help – RbG Feb 20 '13 at 12:26

2 Answers2

2

Check for any output(echo) or whitespaces in db.inc.php.

You must not output anything before calling header()

Also, encode your files into UTF-8 without BOM.

sybear
  • 7,837
  • 1
  • 22
  • 38
  • if i want to set charset by mysqli_set_charset() ..it's showing undefined error that's why i removed that portion of code – RbG Feb 20 '13 at 12:24
  • Visible (echoed by system) error is also an ouput. – sybear Feb 20 '13 at 12:26
  • no there is no white space in db.inc .php..please help – RbG Feb 20 '13 at 12:26
  • there is only one error what i mentioned.. – RbG Feb 20 '13 at 12:27
  • First line of a PHP file must start with `` No whitespaces must be before the opening tag and after closing tag. Check again. Or try encoding your file into `utf-8 without bom`. – sybear Feb 20 '13 at 12:30
  • ""try encoding your file into utf-8 without bom"" how i will do it. – RbG Feb 20 '13 at 12:34
0

if you cant find the white space or where your script doing output, place this line at top of your script:

ob_start()

Then, you need this function also before any exit();

ob_end_flush();