Tuesday 30 May 2017

php - Warning: Cannot modify header information - headers already sent by







Hi All,




I keep having a warning message appear when I try login. Can you help me rectify the problem.



Warning: Cannot modify header information - headers already sent by (output started at /path/to/login.php:15) in /path/to/login.php on line 231


here's the php code before the html:



        //allow sessions to be passed so we can see if the user is logged in

session_start();
//connect to the database so we can check, edit, or insert data to our users table
$con = mysql_connect('XXXXXXXXXXXXXXX', 'XXXXXXXXXXXX', 'XXXXXXXXXX') or die(mysql_error());
$db = mysql_select_db('XXXXXXXXXXXXXXXXX', $con) or die(mysql_error());
//include out functions file giving us access to the protect() function made earlier
include "functions.php";
?>


Here is the functions.php file:





function protect($string){
$string = trim(strip_tags(addslashes($string)));
return $string;
}

?>



and finally here is the PHP inside the HTML:




//If the user has submitted the form
if($_POST['submit']){
//protect the posted value then store them to variables
$username = protect($_POST['username']);
$password = protect($_POST['password']);


//Check if the username or password boxes were not filled in
if(!$username || !$password){
//if not display an error message
echo "
You need to fill in a Username and a Password!
";
}else{
//if the were continue checking

//select all rows from the table where the username matches the one entered by the user
$res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."'");

$num = mysql_num_rows($res);

//check if there was not a match
if($num == 0){
//if not display an error message
echo "
The Username you supplied does not exist!
";
}else{
//if there was a match continue checking

//select all rows where the username and password match the ones submitted by the user

$res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."' AND `password` = '".$password."'");
$num = mysql_num_rows($res);

//check if there was not a match
if($num == 0){
//if not display error message
echo "
The Password you supplied does not match the one for that username!
";
}else{
//if there was continue checking


//split all fields fom the correct row into an associative array
$row = mysql_fetch_assoc($res);

//check to see if the user has not activated their account yet
if($row['active'] != 1){
//if not display error message
echo "
You have not yet Activated your account!
";
}else{
//if they have log them in


//set the login session storing there id - we use this to see if they are logged in or not
$_SESSION['uid'] = $row['id'];
//show message
echo "
You have successfully logged in!
";

//update the online field to 50 seconds into the future
$time = date('U')+50;
mysql_query("UPDATE `users` SET `online` = '".$time."' WHERE `id` = '".$_SESSION['uid']."'");

//redirect them to the usersonline page

header('Location: usersOnline.php');
}
}
}
}
}

?>



The main issue arises when I try redirect them to my usersOnline.php page.



//redirect them to the usersonline page
header('Location: usersOnline.php');


I have minimised all white space, but still have problems. Where the error informs me, line 15 is my meta id's (Description & Keywords - SEO). Does this pose problems in PHP?



Any help given is much appreciated as I am relatively new with PHP.




Thanks,
Darren

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...