views:

53

answers:

3

I've created a code to change a password. Now it seem contain an error. When before I fill in the form to change password.the error is:

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\e-Complaint(FYP)\userChangePass.php:7) in C:\Program Files\xampp\htdocs\e-Complaint(FYP)\userChangePass.php on line 126

the code:

<?php # userChangePass.php
//this page allows logged in user to change their password.

$page_title='Change Your Password';

//if no first_name variable exists, redirect the user
if(!isset($_SESSION['userid'])){

    header("Location: http://" .$_SERVER['HTTP_HOST']. dirname($_SERVER['PHP_SELF'])."/index.php");
    ob_end_clean();
    exit();

}else{

    if(isset($_POST['submit'])) {//handle form.
        require_once('connectioncomplaint.php'); //connec to the database

        //check for a new password and match againts the confirmed password.
        if(eregi ("^[[:alnum:]]{4,20}$", stripslashes(trim($_POST['password1'])))){
            if($_POST['password1'] == $_POST['password2']){
                $p =escape_data($_POST['password1']);
            }else{
                $p=FALSE;
                echo'<p><font color="red" size="+1"> Your password did not match the confirmed password!</font></p>';
            }
        }else{
            $p=FALSE;
            echo'<p><font color="red" size="+1"> Please Enter a valid password!</font></p>';
        }

        if($p){ //if everything OK.

            //make the query
            $query="UPDATE access SET password=PASSWORD('$p') WHERE userid={$_SESSION['userid']}";
            $result=@mysql_query($query);//run the query.
            if(mysql_affected_rows() == 1) {//if it run ok.

                //send an email,if desired.
                echo '<p><b>your password has been changed.</b></p>';
                //include('templates/footer.inc');//include the HTML footer.
                exit();

            }else{//if it did not run ok

            $message= '<p>Your password could not be change due to a system error.We apolpgize for any inconvenience.</p><p>' .mysql_error() .'</p>';

            }
            mysql_close();//close the database connection.

            }else{//failed the validation test.
                echo '<p><font color="red" size="+1"> Please try again.</font></p>';
            }
        }//end of the main Submit conditional.

    }
    ?>

the error at this line:-

header("Location: http://" .$_SERVER['HTTP_HOST']. dirname($_SERVER['PHP_SELF'])."/index.php");

please help me guy...

+2  A: 

Usually this sort of error occurs if there is some output before calling header(). This doesn't have to be an explicit echo or print, it could just be a space or a newline before your first php tag. It could also be output from another file that is included in your code or that includes your code.

Check back through all your code to make sure that any accidental output is eliminated before the php tag.

Edit: I just noticed that you're using output buffering. In that case, check for output before ob_start() is called.

Yozomiri
+1  A: 

Check if you have a newline or space in your php tag.

Like

//Cant have space!
<?php
  //code
?>
//Cant have this space!
<?php
  //code
?>
JeremySpouken
A: 

There are two line numbers in the warning message you've got

output started at C:\Program Files\xampp\htdocs\e-Complaint(FYP)\userChangePass.php:7

and

in C:\Program Files\xampp\htdocs\e-Complaint(FYP)\userChangePass.php on line 126
There are less than 126 lines of code in the code snippet you've posted, so ...forget it for now.
Let's see what is on line 7 of the code you've posted

1: <?php # userChangePass.php
2: //this page allows logged in user to change their password.
3: 
4: $page_title='Change Your Password';
5: 
6: //if no first_name variable exists, redirect the user
7: if(!isset($_SESSION['userid'])){

That doesn't match either since there's nothing in that line that would produce any output.
Please post the code again and mark lines 7 and 126 (cut out something in the middle but do include those two lines)

VolkerK