Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

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

Featured Replies

hi guys

this is my code, but I receive Cannot modify header information warning on the line which is Bold and Underlined. tnx

 

<?php
@session_start();
$class="loginTextbox";
if ($_SESSION['url']!="/amlak/index.php")
{
header("location:index.php");
exit ();
}
?>
<?php
if (isset($_POST['password']))
{
require_once './views/database/systemComponent.php';
require_once './views/database/dbConnector.php';
$dbConnectorObject =new dbConnector();
$sentPassword = $_POST['password'];
$select = "SELECT * FROM password";
$result = mysql_query($select)or die(mysql_error());
while($rows=mysql_fetch_array($result))
{
$currentFirstPassword = $rows['password1'];
$currentSecondPassword = $rows['password2'];
}
if(md5($sentPassword)==$currentFirstPassword)
{
$_SESSION['login']=true;
header("location:add.php");
exit();
}
else{
$class="failedloginTextbox";
}
}
?>

Edited by stallman3738

Hi Stallman,


Looking over your code I can only see one real obvious point of call and that is your database, what would be the result if you were to comment out the line you have highlighted?


Do you have display errors enabled in your PHP ini?

  • Author

Hi Stallman,

 

Looking over your code I can only see one real obvious point of call and that is your database, what would be the result if you were to comment out the line you have highlighted?

 

Do you have display errors enabled in your PHP ini?

 

 

yes display errors are enabled.

If comment out the highlighted line then there will be no header and no redirection to add.php page.

Edited by stallman3738

  • Author

You have 'required' 2 files in the views folder. Do either of these files send any HTML to the browser?

 

No, this is systemComponent.php

 

<?php

class systemComponent{

var $setting;

function getSetting(){

$setting['dbhost']='localhost';

$setting[ ' dbusername' ]='root';

$setting['dbpassword']='';

$setting['dbname']='amlak';

return $setting;

}

}

?>

 

and this is dbConnector.php,

 

<?php

require_once 'systemComponent.php';

class dbConnector extends systemComponent{

var $theQuery;

var $link;

function dbConnector(){

$setting=systemComponent::getSetting();

$host=$setting['dbhost'];

$db=$setting['dbname'];

$user=$setting['dbusername'];

$pass=$setting['dbpassword'];

$this->link=mysql_connect($host,$user,$pass)or die(mysql_error());;

mysql_select_db($db) or die(mysql_error());

mysql_set_charset('utf-8');

}

 

function query($query){

$this->theQuery=$query;

return mysql_query($query,$this->link) or die(mysql_error());

}

 

function fetchArray($result){

return mysql_fetch_array($result);

}

 

function close(){

mysql_close($this->link);

}

}

?>

 

is there anything wrong?

hmmm. Is the quoted file standalone or is it being included by something else? I'm not positive but it looks like your code is being included by another script and if that script outputs something to the browser then the redirection won't work. One option would be to use output buffering in the calling script.

 

As an aside, you might want to consider updating your database code to the mysqli extension or use the PDO class, both are pretty easy to learn. mysql is deprecated and I've read that php 5.5 will not support it.

Hello Stallman3738,

 

I can remember I had this same issue,

if I remember it correctly it was caused by whitespace.

 

}
?>

<!-- Here is whitespace. -->

<?php
     if (isset($_POST['password']))

 

The white space between your php code is handled as output.

You could try this:

<?php

// Make sure there is no whitespace before your opening tag.

@session_start();
$class="loginTextbox";
 if ($_SESSION['url']!="/amlak/index.php")
{
    header("location:index.php");
     exit ();
}

// Code merged.

     if (isset($_POST['password']))
    {
            require_once './views/database/systemComponent.php';
            require_once './views/database/dbConnector.php';
            $dbConnectorObject =new dbConnector();
            $sentPassword = $_POST['password'];
            $select = "SELECT * FROM password";
            $result = mysql_query($select)or die(mysql_error());
            while($rows=mysql_fetch_array($result))
            {
                $currentFirstPassword = $rows['password1'];
                $currentSecondPassword = $rows['password2'];
            }
            if(md5($sentPassword)==$currentFirstPassword)
            {
                $_SESSION['login']=true;
                header("location:add.php");       
                exit();
            }
            else{
                $class="failedloginTextbox";
            }
    }
?>

 

Hope it helps,

Léon

Edited by Leonvv

  • Author

Hello Stallman3738,

 

I can remember I had this same issue,

if I remember it correctly it was caused by whitespace.

 

}
?>

<!-- Here is whitespace. -->

<?php
     if (isset($_POST['password']))

 

The white space between your php code is handled as output.

You could try this:

<?php

// Make sure there is no whitespace before your opening tag.

@session_start();
$class="loginTextbox";
 if ($_SESSION['url']!="/amlak/index.php")
{
    header("location:index.php");
     exit ();
}

// Code merged.

     if (isset($_POST['password']))
    {
            require_once './views/database/systemComponent.php';
            require_once './views/database/dbConnector.php';
            $dbConnectorObject =new dbConnector();
            $sentPassword = $_POST['password'];
            $select = "SELECT * FROM password";
            $result = mysql_query($select)or die(mysql_error());
            while($rows=mysql_fetch_array($result))
            {
                $currentFirstPassword = $rows['password1'];
                $currentSecondPassword = $rows['password2'];
            }
            if(md5($sentPassword)==$currentFirstPassword)
            {
                $_SESSION['login']=true;
                header("location:add.php");       
                exit();
            }
            else{
                $class="failedloginTextbox";
            }
    }
?>

 

Hope it helps,

Léon

 

No I tested this and still does not work;

There was something much more strange, I tested this

 

<?php header("location:add.php"); ?>

 

and still received the same warning

Do you have any whitespace before the <? part ?

 

Léon

There was something much more strange, I tested this

 

<?php header("location:add.php"); ?>

 

and still received the same warning

This error can be caused by a variety of things. Html output, php echos before the redirect,whitespace as mentioned,include files that contain any of the stuff I just mentioned. You basically just need to do some debugging and make sure you don't have any of the above. As an alternative you could try this

 

<?php
if(!headers_sent()){
header('Location:index.php');
exit;
}else{
//Headers sent use javascript instead
echo "<script>window.location = \"index.php\"</script>";
}

?>

Edited by webdesigner93

  • Author

 

This error can be caused by a variety of things. Html output, php echos before the redirect,whitespace as mentioned,include files that contain any of the stuff I just mentioned. You basically just need to do some debugging and make sure you don't have any of the above. As an alternative you could try this

 

<?php
if(!headers_sent()){
header('Location:index.php');
exit;
}else{
//Headers sent use javascript instead
echo "<script>window.location = \"index.php\"</script>";
}
?>

 

 

Thanks alot it worked, how do I output the header_sent data to see what they are?

No, this is systemComponent.php

 

<?php

class systemComponent{

var $setting;

function getSetting(){

 

is there anything wrong?

 

"var" is depreciated and from PHP 5.0.0 through to 5.1.2 it will throw an error if used with E_STRICT errors enabled. This might be where your headers/output are coming from if you're using those versions.

 

Also, the main reason why I'm replying; please, please, PLEASE tell me that you are not solely using MD5 for your password encryption methods?! It is categorically flawed and holds many weaknesses that allow it to be easily cracked. You should be using something with a stronger encryption method, specifically with salting. See crypt() and PHPass - which actually uses crypt and takes a lot of the nitty-gritty out of using it.

  • Author

"var" is depreciated and from PHP 5.0.0 through to 5.1.2 it will throw an error if used with E_STRICT errors enabled. This might be where your headers/output are coming from if you're using those versions.

 

Also, the main reason why I'm replying; please, please, PLEASE tell me that you are not solely using MD5 for your password encryption methods?! It is categorically flawed and holds many weaknesses that allow it to be easily cracked. You should be using something with a stronger encryption method, specifically with salting. See crypt() and PHPass - which actually uses crypt and takes a lot of the nitty-gritty out of using it.

 

 

Hi,

I found it, because I didn't save the file in UTF-8, it was in ANSI that is why that error reported. besides that I think it would be a good idea to redirect using .htaccess file to overcome such problems in situations where you can't find the solution.

 

and about md5, yeah I have used this as the only method to encrypt the password; thanks for your suggestion ;)

Edited by stallman3738

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.