John John - 3 months ago 9
PHP Question

Cannot modify header information - headers already sent

So I have a basic login system. I am trying to use a header to redirect after a successful login. But I am getting a error printed saying "Cannot modify header" I am not entirely familiar with PHP at all. But all help would be appreciated.

the error message

Warning: Cannot modify header information - headers already sent by (output started at /home4/jachun39/public_html/ap/login.php:6) in /home4/jachun39/public_html/ap/login.php on line 22


login.php file

<?php
include('SqlConnect.php');
?>
<?php
if (isset($_POST['Login'])){
if (!@mysql_connect($host, $username, $password)) die("Can't connect to database");
if (!mysql_select_db($db_name)) die("Can't select database");
$username=$_POST['username'];
$password=$_POST['password'];
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count >= 1){
$_SESSION['username']= "username";
$_SESSION['password']= "password";
header("location: index.php?message=success");
echo "<center><font color='green'><b>Logged in Successfully</center></font></b>";
} else {
echo "<center><font color='red'><b>Wrong username or password</center></font></b>";
}
}
<link rel="Stylesheet" type="text/css" href="style.css" />
<link href='http://fonts.googleapis.com/css?family=Karla:400,700,700italic,400italic' rel='stylesheet' type='text/css'>


Here is also my Checklogin.php file, which is "included" at the top of the index page.

<?php
if(!isset($_SESSION['username'])){
header("location: login.php");
}
?>


///ADDED AS REQUESTED (index.php)

<?php
ob_start();
include('CheckLogin.php');
include('SqlConnect.php');
include('Userbar.php');
?>
<link rel="Stylesheet" type="text/css" href="style.css" />
<link href='http://fonts.googleapis.com/css?family=Karla:400,700,700italic,400italic' rel='stylesheet' type='text/css'>
</html>
<body>
<br>
<form name="form" method="POST" action=""><td>
<table width="325" border="0" align="center" cellpadding="2" cellspacing="0" bgcolor="#212121">
<td><table width="100%" border="0" cellpadding="3" cellspacing="0" bgcolor="#404040"></td>
<tr colspan="3"><strong> <font color="ECECEC"> Create Code </font></strong></tr>
<tr>
<td><center><font color="ECECEC">Code: <input name="code" type="text"></font>
<input type="Submit" value="Create Code" name="Addcode" /></td></tr></center>
</table></table>
</table></table>
</form>
<?php
if (!mysql_connect($host, $username, $password)){
die("Can't connect to database");
}
if (!mysql_select_db($db_name)){
die("Can't select database");
}
if (isset($_POST['Addcode'])){
mysql_query("INSERT INTO $table (username, password, hwid, ip, code, banned) VALUES('-','-','-','-','$_POST[code]','0')");
}
if (isset($_POST['Action'])){
if ($_POST['Action'] == "Delete"){
$ID = $_POST['ID'];
$result = mysql_query("DELETE FROM $table WHERE `id` = $ID");
if (!$result){
die(mysql_error());
}
} elseif ($_POST['Action'] == "Ban"){
$ID = $_POST['ID'];
$Banned = $_POST['Banned'];
if ($Banned == 0){
$result = mysql_query("UPDATE $table SET `banned` = '1' WHERE `id` = '".$ID."'");
} else {
$result = mysql_query("UPDATE $table SET `banned` = '0' WHERE `id` = '".$ID."'");
}
if (!$result){
die(mysql_error());
}
}
}
echo "<div class='table' align='center'><table><tr class='top'>";
echo "<td \"Username\"'>Username<style='margin-bottom:-1px; float:right;' height='16px' width='16px' /></td>";
echo "<td \"Password\"'>Password<style='margin-bottom:-1px; float:right;' height='16px' width='16px' /></td>";
echo "<td \"Hwid\"'>Hwid<style='margin-bottom:-1px; float:right;' height='16px' width='16px' /></td>";
echo "<td \"Code\"'>Code<style='margin-bottom:-1px; float:right;' height='16px' width='16px' /></td>";
echo "<td \"IP\"'>IP<style='margin-bottom:-1px; float:right;' height='16px' width='16px' /></td>";
echo "<td \"Active\"'>Active<style='margin-bottom:-1px; float:right;' height='16px' width='16px' /></td>";
echo "<td \"Delete\"'>Delete<style='margin-bottom:-1px; float:right;' height='16px' width='16px' /></td>";
echo "</tr>\n";
$type = "first";
$query = mysql_query("SELECT * FROM $table");
while($row = mysql_fetch_array($query)){
$ID = $row['id'];
$Username = $row['username'];
$Password = $row['password'];
$Hwid = $row['hwid'];
$Code = $row['code'];
$IP = $row['ip'];
$Banned = $row['banned'];
$Delete = "0";
echo '<td>'.$Username.'</td><td>'.$Password.'</td><td>'.$Hwid.'</td><td>'.$Code.'</td><td>'
.$IP.'</td><td>';
?>
<form action="" class="form" method="POST">
<input type="hidden" name="Action" value="Ban" />
<input type="hidden" name="ID" value=<?php echo "$ID";?> />
<input type="hidden" name="Banned" value=<?php echo "$Banned";?> />
<?php
if ($Banned == 1){
echo '<input type="image" src="img/cross.png" name="Ban" />';
} else {
echo '<input type="image" src="img/tick.png" name="Ban" />';
}
?>
</td><td>
</form>
<form action="" class="form" method="POST">
<input type="hidden" name="Action" value="Delete" />
<input type="hidden" name="ID" value=<?php echo "$ID";?> />
<input type="image" src="img/delete.png" name="Delete" />
</form>
</td>
<?php
echo "</tr>\n";
}
?>
</body>
</html>

Answer

put this line on top where your php tags start.

ob_start();

Like here you can:

<?php
ob_start();
include('SqlConnect.php');
?>