Jump to content

Header already sent error message on LIVE server


thara

Recommended Posts

After uploading my PHP project to a live server, I get this error message when accessing some pages.
 

    Warning: Cannot modify header information - headers already sent by (output started at /home/mychoice/public_html/version-2/dashboard/includes/header.inc.html:14) in /home/mychoice/public_html/version-2/dashboard/modules/_modify_admin_select.inc.php on line 63

 

But when running on localhost, I do not get this error and every script is working properly.

I have checked white spaces, PHP opening closing tags, and Encoding without BOM.

But I still couldn't figure this out.

This is my header.inc.html page:
 

<?php # -- header.html
// This page begins the HTML header for the site.

// Check for a $page_title value:
if (!isset($page_title)) $page_title = 'Computer store | Control Panel';
?><!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title><?php echo $page_title; ?></title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link rel="icon" type="image/ico" href="favicon.ico"/>
        <link rel="apple-touch-icon" sizes="152x152" href="apple-touch-icon-precomposed.png" />

        <link rel="stylesheet" href="css/styles.css">

        <script src="js/vendor/modernizr.min.js"></script>
    </head>
    <body>
      <!--[if lt IE 7]>
        <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
      <![endif]-->

            <!-- End of header. -->
Link to comment
Share on other sites

The first thing that jumps to mind is that it's likely something that is being output by the calling script rather than the header script.  Looking at the line in the error : _modify_admin_select.inc.php on line 63  I would start by checking those first 62 lines to see if there is anything at all that could be getting output to the page before the header script is being called (including any spurious line breaks outside <php ?> tags and any possible warning/error messages). 

Link to comment
Share on other sites

I have used header() on line 63 in  _modify_admin_select.inc.php page. But I am not what is the problem of this script.

 

This is my code of _modify_admin_select.inc.php :

<?php

// Check login status, If not redirect to login page
if (!login_check($mysqli) == true) {
    $url = BASE_URL.BASE_URI.'index.php';        
    // Define the URL.
    header("Location: $url");
    exit( ); // Quit the script.
}

// Check login user is super admin or not
if ($_SESSION['member_type'] != 1) {
    echo "<h2>You are not authorized to access this page.</h2>";
    exit();
}

// Error Flag:
$error_msg = "";

// Fetch username along with email or domain name
$query = "SELECT
                        member_id,
                        username,
                        email
                    FROM
                        members     
                    WHERE member_type = 0
                    ORDER BY username ASC";
    
$results = $mysqli->query($query);

// If $results is true
if ($results == true) {

    // flag variables for <option>
    $email         = '';
    $username = '';
    
    // Fetch result:
    while($row = $results->fetch_array(MYSQLI_NUM)) {    
        $username .= "<option value=\"$row[0]\" data-userId=\"uid$row[0]\">$row[1]</option>\n";   
        $email      .= "<option value=\"$row[0]\" data-userId=\"uid$row[0]\">$row[2]</option>\n";
    }
} else {
        // If $results not true
        $error_msg .= 'Database error';
}

// Check for a form submission:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Check for E-mail or Domain and username are selected from dropdown lists:
    if ( isset ( $_POST['email'], $_POST['userName'])) {
        $userid = $_POST['email'];                
        //echo $userid;    
        
        // XSS protection as we might print this value
    $userid = preg_replace("/[^0-9]+/", "", $userid);
    $_SESSION['userid'] = $userid;

        // redirect to the next page
        $url = BASE_URL.BASE_URI. 'index.php?p=modify-admin';        
                     // Define the URL.
                     header("Location: $url");
                     exit(); // Quit the script.            
            
    } else {
        $error_msg .= ' You didn\' select either username or email to modify.';
    }
}

// include main navigation links
include_once('includes/main-nav.inc.html');
?>


<div id="page-wrapper" >

    <!-- display breadcrumb links -->
    <ol class="breadcrumb">
        <li><a href="index.php?p=account-function"><i class="fa fa-home"></i> Home</a></li>
        <li><a href="index.php?p=account-function">Account Function</a></li>
        <li class="active">Modify an Admin</li>
    </ol>
    
    <div id="page-inner">
        <div class="row">
            <div class="col-md-12">
                <h2>Modify an Admin</h2><br>
            </div>
        </div>              
        <!-- /. ROW  -->
        
        <div class="statistics">
                    
        <?php if (!empty($error_msg)) : ?>                            
            <div class="user-msg">
                <div class="alert alert-danger alert-dismissible fade in" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
                    <strong>Oops ! </strong> <?php echo htmlentities($error_msg); ?>
                </div>
            </div>
        <?php endif; ?>

        <div class="details">  
            <div class="panel panel-details">
                <div class="panel-heading"><h3 class="panel-title">Account Selection</h3></div>
                <table class="table table-striped">
                    <thead>
                        <tr class="active">
                            <th>Domains / Emails</th>
                            <th>Username</th>
                        </tr>
                    </thead>
                    <tbody>
                        <form action="" method="post">
                            <tr>   
                                <td>                        
                                    <select class="form-control" size="8" id="email" name="email">
                                        <?php echo $email; ?>
                                    </select>
                                </td>
                                <td>
                                    <select class="form-control" size="8" id="userName" name="userName">
                                        <?php echo $username; ?>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <br>
                                    <button type="submit" class="btn btn-primary" >Modify</button>
                                    <br><br>
                                </td>
                                <td></td>
                            </tr>
                        </form>                                        
                    </tbody>
                </table>
            </div>
        </div>
        </div> <!-- /.details -->    
    <!-- /. PAGE INNER  -->
    </div>
 <!-- /. PAGE WRAPPER  -->
</div>
</div>
<!-- /. WRAPPER  -->

Hope somebody may help me out. Thank you.

Link to comment
Share on other sites

This is how I included header.inc.html file in my index.php page.

<?php # -- index.php

/*
 *  This is the main page.
 *  This page includes the configuration file,
 *  the templates, and any content-specific modules.
 */

// Require the configuration file before any PHP code:
require('./includes/configuration.inc.php');

// Need the database connection:
require(MYDB);

// Our custom secure way of starting a PHP session.
sec_session_start();

// Validate what page to show:
if (isset($_GET['p'])) {
    $p = $_GET['p'];
} elseif (isset($_POST['p'])) { // Forms
    $p = $_POST['p'];
} else {
    $p = NULL;
}

// Determine what page to display:
switch ($p) {
                            
    case 'modify-admin-select':
        $page = '_modify_admin_select.inc.php';
        $page_title = 'MyChoice Computers | Control Panel - Modify an Admin';
        break;    
                
        case 'modify-admin':
        $page = '_modify_admin.inc.php';
        $page_title = 'MyChoice Computers | Control Panel - Modify an Admin';
        break;            
    
    // Default is to include the main page.
    default:
        $page = 'main.inc.php';
        $page_title = 'Site Home Page';
        break;
        
} // End of main switch.

// Make sure the file exists:
if (!file_exists('./modules/' . $page)) {
    $page = 'login.inc.php';
    $page_title = 'MyChoice Computers | Control Panel Login';
}

// Include the header file:
include('./includes/header.inc.html');

// Include the content-specific module:
// $page is determined from the above switch.
include('./modules/' . $page);

// Include the footer file to complete the template:
include('./includes/footer.inc.html');

 

Link to comment
Share on other sites

errrr... many ways... I'd suggest studying the MVC pattern, but a quick solution:

 

Move the following:

// Include the header file:
include('./includes/header.inc.html');

to after the possible header() clause in the $page pages... e.g here:

 

<?php

// Check login status, If not redirect to login page
if (!login_check($mysqli) == true) {
$url = BASE_URL.BASE_URI.'index.php';
// Define the URL.
header("Location: $url");
exit( ); // Quit the script.
}

// Include the header file:
include('./includes/header.inc.html');

 

Others may suggest using ob_start and such.

* In Wordpress themes, each page template use an include for the header section

 

 

Why I suggest the MVC approach is because you work out all the bits of the Model before outputting the View.

For instance, on page request you check the page request link, then handle any form bits (from the Controller, i.e. the response from the View), and then output the View. For more see the Application Design forum here on PHPFreaks: http://forums.phpfreaks.com/topic/20556-resources/

 

Also from that same page here's a link about how to use the cache (ob_start, etc): http://www.devshed.com/c/a/PHP/Output-Caching-with-PHP/

Link to comment
Share on other sites

I tried this with previous solution @IThinkMyBrainHurts provided. But problem is still same. I replace header.inc.html from my index.php page.

 

This is my updated _modify_admin_select.inc.php page:

<?php

// Check login status, If not redirect to login page
if (!login_check($mysqli) == true) {
    $url = BASE_URL.BASE_URI.'index.php';        
    // Define the URL.
    header("Location: $url");
    exit(); // Quit the script.
}

// Include the header file:
include('./includes/header.inc.html');

// Check login user is super admin or not
if ($_SESSION['member_type'] != 1) {
    echo "<h2>You are not authorized to access this page.</h2>";
    exit();
}

// Error Flag:
$error_msg = "";

// Fetch username along with email or domain name
$query = "SELECT
                        member_id,
                        username,
                        email
                    FROM
                        members     
                    WHERE member_type = 0
                    ORDER BY username ASC";
    
$results = $mysqli->query($query);

// If $results is true
if ($results == true) {

    // flag variables for <option>
    $email         = '';
    $username = '';
    
    // Fetch result:
    while($row = $results->fetch_array(MYSQLI_NUM)) {    
        $username .= "<option value=\"$row[0]\" data-userId=\"uid$row[0]\">$row[1]</option>\n";   
        $email      .= "<option value=\"$row[0]\" data-userId=\"uid$row[0]\">$row[2]</option>\n";
    }
} else {
        // If $results not true
        $error_msg .= 'Database error';
}

// Check for a form submission:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Check for E-mail or Domain and username are selected from dropdown lists:
    if ( isset ( $_POST['email'], $_POST['userName'])) {
        $userid = $_POST['email'];                
        //echo $userid;    
        
        // XSS protection as we might print this value
    $userid = preg_replace("/[^0-9]+/", "", $userid);
    $_SESSION['userid'] = $userid;

        // redirect to the next page
        $url = BASE_URL.BASE_URI. 'index.php?p=modify-admin';        
                     // Define the URL.
                     header("Location: $url");
                     exit(); // Quit the script.        
                    
                    
// Include the header file:
include('./includes/header.inc.html');    
            
    } else {
        $error_msg .= ' You didn\' select either username or email to modify.';
    }
}

// include main navigation links
include_once('includes/main-nav.inc.html');
?>


<div id="page-wrapper" >

    <!-- display breadcrumb links -->
    <ol class="breadcrumb">
        <li><a href="index.php?p=account-function"><i class="fa fa-home"></i> Home</a></li>
        <li><a href="index.php?p=account-function">Account Function</a></li>
        <li class="active">Modify an Admin</li>
    </ol>
    
    <div id="page-inner">
        <div class="row">
            <div class="col-md-12">
                <h2>Modify an Admin</h2><br>
            </div>
        </div>              
        <!-- /. ROW  -->
        
        <div class="statistics">
                    
        <?php if (!empty($error_msg)) : ?>                            
            <div class="user-msg">
                <div class="alert alert-danger alert-dismissible fade in" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
                    <strong>Oops ! </strong> <?php echo htmlentities($error_msg); ?>
                </div>
            </div>
        <?php endif; ?>

        <div class="details">  
            <div class="panel panel-details">
                <div class="panel-heading"><h3 class="panel-title">Account Selection</h3></div>
                <table class="table table-striped">
                    <thead>
                        <tr class="active">
                            <th>Domains / Emails</th>
                            <th>Username</th>
                        </tr>
                    </thead>
                    <tbody>
                        <form action="" method="post">
                            <tr>   
                                <td>                        
                                    <select class="form-control" size="8" id="email" name="email">
                                        <?php echo $email; ?>
                                    </select>
                                </td>
                                <td>
                                    <select class="form-control" size="8" id="userName" name="userName">
                                        <?php echo $username; ?>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <br>
                                    <button type="submit" class="btn btn-primary" >Modify</button>
                                    <br><br>
                                </td>
                                <td></td>
                            </tr>
                        </form>                                        
                    </tbody>
                </table>
            </div>
        </div>
        </div> <!-- /.details -->    
    <!-- /. PAGE INNER  -->
    </div>
 <!-- /. PAGE WRAPPER  -->
</div>
</div>
<!-- /. WRAPPER  -->
Link to comment
Share on other sites

You are including the header file twice.  once near the top of the script, and once again further down.  As your code is there it seems to be lines 12 and 71.  You could try changing your include() calls to require_once() and see if that helps...also, remove duplicate calls to the file regardless.  Let us know if that helps.

Link to comment
Share on other sites

@Muddy_Funster, I did it as you said. But still I can get that error message -

 

This is my update page :

<?php

// Check login status, If not redirect to login page
if (!login_check($mysqli) == true) {
    $url = BASE_URL.BASE_URI.'index.php';        
    // Define the URL.
    header("Location: $url");
    exit(); // Quit the script.
}

// Include the header file:
require_once('./includes/header.inc.html');

// Check login user is super admin or not
if ($_SESSION['member_type'] != 1) {
    echo "<h2>You are not authorized to access this page.</h2>";
    exit();
}

// Error Flag:
$error_msg = "";

// Fetch username along with email or domain name
$query = "SELECT
                        member_id,
                        username,
                        email
                    FROM
                        members     
                    WHERE member_type = 0
                    ORDER BY username ASC";
    
$results = $mysqli->query($query);

// If $results is true
if ($results == true) {

    // flag variables for <option>
    $email         = '';
    $username = '';
    
    // Fetch result:
    while($row = $results->fetch_array(MYSQLI_NUM)) {    
        $username .= "<option value=\"$row[0]\" data-userId=\"uid$row[0]\">$row[1]</option>\n";   
        $email      .= "<option value=\"$row[0]\" data-userId=\"uid$row[0]\">$row[2]</option>\n";
    }
} else {
        // If $results not true
        $error_msg .= 'Database error';
}

// Check for a form submission:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Check for E-mail or Domain and username are selected from dropdown lists:
    if ( isset ( $_POST['email'], $_POST['userName'])) {
        $userid = $_POST['email'];                
        //echo $userid;    
        
        // XSS protection as we might print this value
    $userid = preg_replace("/[^0-9]+/", "", $userid);
    $_SESSION['userid'] = $userid;

        // redirect to the next page
        $url = BASE_URL.BASE_URI. 'index.php?p=modify-admin';        
                     // Define the URL.
                     header("Location: $url");
                     exit(); // Quit the script.            
            
    } else {
        $error_msg .= ' You didn\' select either username or email to modify.';
    }
}

// include main navigation links
include_once('includes/main-nav.inc.html');
?>


<div id="page-wrapper" >

    <!-- display breadcrumb links -->
    <ol class="breadcrumb">
        <li><a href="index.php?p=account-function"><i class="fa fa-home"></i> Home</a></li>
        <li><a href="index.php?p=account-function">Account Function</a></li>
        <li class="active">Modify an Admin</li>
    </ol>
    
    <div id="page-inner">
        <div class="row">
            <div class="col-md-12">
                <h2>Modify an Admin</h2><br>
            </div>
        </div>              
        <!-- /. ROW  -->
        
        <div class="statistics">
                    
        <?php if (!empty($error_msg)) : ?>                            
            <div class="user-msg">
                <div class="alert alert-danger alert-dismissible fade in" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
                    <strong>Oops ! </strong> <?php echo htmlentities($error_msg); ?>
                </div>
            </div>
        <?php endif; ?>

        <div class="details">  
            <div class="panel panel-details">
                <div class="panel-heading"><h3 class="panel-title">Account Selection</h3></div>
                <table class="table table-striped">
                    <thead>
                        <tr class="active">
                            <th>Domains / Emails</th>
                            <th>Username</th>
                        </tr>
                    </thead>
                    <tbody>
                        <form action="" method="post">
                            <tr>   
                                <td>                        
                                    <select class="form-control" size="8" id="email" name="email">
                                        <?php echo $email; ?>
                                    </select>
                                </td>
                                <td>
                                    <select class="form-control" size="8" id="userName" name="userName">
                                        <?php echo $username; ?>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <br>
                                    <button type="submit" class="btn btn-primary" >Modify</button>
                                    <br><br>
                                </td>
                                <td></td>
                            </tr>
                        </form>                                        
                    </tbody>
                </table>
            </div>
        </div>
        </div> <!-- /.details -->    
    <!-- /. PAGE INNER  -->
    </div>
 <!-- /. PAGE WRAPPER  -->
</div>
</div>
<!-- /. WRAPPER  -->

This is a link for my real project. But on localhost this script working correctly.

Edited by thara
Link to comment
Share on other sites

yes.. On live server I have made friendly error message. But real message come to my email.

 

This is real message after updating the script :

 

An error occurred in script '/home/mychoice/public_html/version-2/dashboard/modules/_modify_admin_select.inc.php' on line 66: Cannot modify header information - headers already sent by (output started at /home/mychoice/public_html/version-2/dashboard/includes/header.inc.html:14)

 

Edited by thara
Link to comment
Share on other sites

you need to check your paths: I get this when trying to follow through to your header.inc.htm file:

Not Found

The requested URL /version-2/dashboard/includes/header.inc.htm was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

 

make sure that the file structure is the same, and that the proper permissions are set on the servers filesystem.

Link to comment
Share on other sites

1. All worked for me, no errors

 

2. Followed latest URL and the the PHP in the HTML file wasn't being parsed! That probably means your server isn't configured to parse PHP within HTML files!

- Simple solution, rename files to .php

- Add the right AddType config, see examples here: http://stackoverflow.com/questions/6295141/server-not-parsing-html-as-php

Link to comment
Share on other sites

browsing to the included files isn't relevant. in fact, the included files should be stored in a location on the server that cannot be reached via a HTTP request, especially now that you have posted links to them and the major search engines have indexed them.

 

the reason your code functioned (i hesitate to use the word 'worked') on your development system is because php thought it would be funny to hide some basic code and page layout problems and allow poorly written code to function. your development system has the output_buffering setting turned on in the php.ini. i recommend that you turn it off so that code you get to work on your development system won't break just because you moved it to another server. once you make this change, you will be able to fix your code on your development system and it won't have this problem when moving it to your live server.

 

a properly laid out web page must send any http headers before sending anything else to the browser. ALL the html tags, starting with the <!DOCTYPE tag is output that goes to the browser. Any header() statement in your php code must come before even the <!DOCTYPE tag.

 

the solution to this problem is to reorganize your code so that the php control logic that determines what to do on the page, such as redirecting if the user isn't logged in or doesn't have permission to access the page, comes first. the html document your page builds and outputs shouldn't even be started until after the point where you have determined that the current visitor can even access the page.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.