Hi please help to get rid of this error/warning...
Please see the following code carefully..
Look at the errors first
-------------------------------------------
Warning: Cannot modify header information - headers already sent by (output started at /home/compkcom/public_html/includes/config.php:1) in /home/compkcom/public_html/cmpanel/check_login.php on line 20

-------------------------------------------
here goes the php script
---------------

<?php
session_start();
?>
<?php
if((isset($_POST['username'])&&$_POST['username']!="")&&(isset($_POST['password'])&&$_POST['password']!="")){
	$error='';	
	require('../includes/config.php');
	$conn = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS) or die('Could not connect to MySQL database. ' . mysql_error());
	mysql_select_db(SQL_DB,$conn);
	$query = "SELECT COUNT(username) AS record FROM admin WHERE username ='" . $_POST['username']."' AND password = '".$_POST['password']."'";
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);
	if($row['record']==1){
		$_SESSION['user_logged'] = $_POST['username'];
		$_SESSION['user_password'] = $_POST['password'];
		header("location:welcome.php");
	}
	else{
		$error .="Please+Enter+Correct+Username+and+password%21%0D%0A";
		header("location:index.php?&error=".$error);
	}
}
else{
		$error .="Please+Enter+the+Username+and+password+First%21%0D%0A";
		header("location:index.php?&error=".$error);
}
?>

----------------
This script is working fine on my local window server, but on my linux hosted server it is giving warning..
Warning is coming because because of inclusion of require('../includes/config.php');
but inclusion of this file is necessary, please help to solve this issue..
or Suggest some alternatives
Thanks..
God bless you..

Dani AI

Generated

The warning means some bytes are being sent to the browser before you call header(). correctly guessed an invisible output (extra space or a Unicode BOM) in the included config.php, and showed ob_start() which can hide the symptom but does not fix the root cause.

Quick practical checks and fixes:

  • Open config.php in a plain-text editor and ensure the file begins immediately with <?php (no blank lines or spaces before it). Save the file as "UTF-8 without BOM" or ASCII. If the file contains only PHP, omit the closing ?> to avoid accidental trailing newlines.
  • To detect and remove a UTF-8 BOM programmatically (make a backup first), use something like:
$path = __DIR__ . '/includes/config.php';
$contents = file_get_contents($path);
if (substr($contents, 0, 3) === "\xEF\xBB\xBF") {
    file_put_contents($path, substr($contents, 3)); // removes BOM
}
  • On the server you can inspect the first bytes with hexdump -C config.php | head or xxd -g 1 -l 16 config.php to confirm BOM bytes EF BB BF.

Why this happens and small extra tips:

  • Any output (even an invisible BOM or a single newline) will lock headers; see PHP header() docs for details.
  • ob_start() (as suggested) will buffer output and can be a temporary workaround, but the clean solution is to remove the stray output.
  • After a header("Location: ...") you should generally stop script execution (exit) so nothing else sends output.
  • While fixing, also note security: avoid storing plaintext passwords and avoid deprecated mysql_* calls — use password_hash/password_verify and mysqli or PDO instead (password_hash docs).

Make a backup before editing config.php. Once the BOM/whitespace is gone the headers warning will disappear.

Recommended Answers

All 5 Replies

Hi....

try to check this below code...

<?php
ob_start();
session_start();

if((isset($_POST['username'])&&$_POST['username']!="")&&(isset($_POST['password'])&&$_POST['password']!="")){
	$error='';	
	require_once('../includes/config.php');
	$conn = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS) or die('Could not connect to MySQL database. ' . mysql_error());
	mysql_select_db(SQL_DB,$conn);
	$query = "SELECT COUNT(username) AS record FROM admin WHERE username ='" . $_POST['username']."' AND password = '".$_POST['password']."'";
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);
	if($row['record']==1){
		$_SESSION['user_logged'] = $_POST['username'];
		$_SESSION['user_password'] = $_POST['password'];
		header("location:welcome.php");
	}
	else{
		$error .="Please+Enter+Correct+Username+and+password%21%0D%0A";
		header("location:index.php?error=".$error."");
	}
}
else{
		$error .="Please+Enter+the+Username+and+password+First%21%0D%0A";
		header("location:index.php?error=".$error);
}
?>

The problem is not in the check_login.php file. The error is in your config.php file.

You can NOT have ANY output from php prior to calling the header function. I assume you have something like the following in your config.php file:

<?php
//Ignore the next line, it just reproduces your error.
header('location: http://www.google.com');

notice that there is a space before the <?php tag.
This is the error message it produces:

Warning: Cannot modify header information - headers already sent by (output started at ****\headerCheck.php:1) in ****\headerCheck.php on line 2

Dear Sir,
Fist of all thank you very much for replying..
Please look at the code of config.php
-------------------------------------

<?php define('SQL_HOST','localhost');define('SQL_USER','compcom_care');define('SQL_PASS','ptk');define('SQL_DB','compcom_ccc');?>

-------------------------------------

Pease look at this config .php code
& now tell the problem
Thanks
God bless you..

Download the attached file and try it. All i did was copy the code from your posting into it and I noticed a weird unicode glitch i've seen before. When I saved the file correcting for it, the glitch went away, it might be the problem you're having.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.