views:

67

answers:

4

Possible Duplicate:
Cannot modify header information - headers already sent, Why its happening

Hi All ,

this is related to my previous question and just because I cant add this code, I'm putting this as a new question

My Question is when I'm trying to redirect the below php page it gives me this error

Warning: Cannot modify header information - headers already sent by (output started at login.php:5) in /login.php on line 17

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    <html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head>
    <title>Administration Dashboard</title>
    <?php
                  require_once 'db/db_functions.inc';
                  require_once 'functions/user_functions.inc';
                  require_once 'functions/string_functions.inc';
                  require_once 'functions/messages.inc';
                  require_once 'functions/login.inc';
                  $message_count = -1;
                  $messages = array();
                  if (!isset ($_POST['Submit']) || $_POST['Submit'] != 'Login'){
                        $login = $_POST['login'];
                        $password = $_POST['password'];
                        if ((do_login($login,  encrypt_password($password))) > 0){
                            header("dashboard");
                        }
                        else{
                            $message_count = 1;
                        }
                  }
    ?>
            <link rel="stylesheet" type="text/css" href="stylesheets/admin/theme.css" />
            <link rel="stylesheet" type="text/css" href="stylesheets/admin/style.css" />
            <link rel="stylesheet" type="text/css" href="stylesheets/admin/theme1.css" />
            <!--[if IE]>
            <link rel="stylesheet" type="text/css" href="stylesheets/admin/ie-sucks.css" />
            <![endif]-->

    </head>

    <body>
        <div id="container">
            <div id="header">
                <h2>Sierra - Login</h2>
          </div>
          <div id="wrapper">
                <div id="content">
                    <div id="box">
                        <?
                            if ($message_count != -1){
                               if ($message_count > 0){
                                   create_login_error_message($messages);
                               }
                            }
                        ?>
                        <form id="form" action="login.php" method="post">
                          <fieldset id="personal">
                            <legend>LOGIN INFORMATION</legend>
                            <label for="lastname">Login : </label>
                            <input name="login" id="login" type="text" tabindex="1" />

                            <br />
                            <label for="pass">Password : </label>
                            <input name="password" id="password" type="password"
                            tabindex="2" />
                            <br />
                          </fieldset>
                          <div align="center">
                          <input id="button1" type="submit" value="Login" />
                          or <a href="">Cancel</a>
                          </div>
                        </form>
                  </div>
                </div>
          </div>
            <div id="footer">
                    <!-- footer -->
                     <?
                        require_once('includes/footer.inc');
                     ?>
            </div>
    </div>
    </body>

</html>
+5  A: 

Because you've already sent HTML to the browser, the HTTP headers have been sent. This is why your error is "Cannot modify header information." Once you send any bytes past the headers, the HTTP headers can't be modified.

Try putting your PHP block before the HTML:

<?php
                  require_once 'db/db_functions.inc';
                  require_once 'functions/user_functions.inc';
                  require_once 'functions/string_functions.inc';
                  require_once 'functions/messages.inc';
                  require_once 'functions/login.inc';
                  $message_count = -1;
                  $messages = array();
                  if (!isset ($_POST['Submit']) || $_POST['Submit'] != 'Login'){
                        $login = $_POST['login'];
                        $password = $_POST['password'];
                        if ((do_login($login,  encrypt_password($password))) > 0){
                            header("dashboard");
                        }
                        else{
                            $message_count = 1;
                        }
                  }
    ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
simeonwillbanks
Also an exit; after header();
daniels
@simeonwillbanks, thanks it worked :D
sameera207
Awesome, glad I could help.
simeonwillbanks
+1  A: 

You need to make sure that nothing is written to the browser before the header() call.

Also, if you are trying to redirect, the call should be: header('Location: url').

Rocket
+1  A: 

You can't send headers after sending HTML to the browser. An easy fix would be to add <? ob_start() ?> at the beginning of the file.

Pies
A: 

Try to Put :

              require_once 'db/db_functions.inc';
              require_once 'functions/user_functions.inc';
              require_once 'functions/string_functions.inc';
              require_once 'functions/messages.inc';
              require_once 'functions/login.inc';

These lines before the head tag and all those including and see if there is any extra spaces before and after the <% or %> tags of php then remove them i think it is happening because your sending header information again and again which is already sent by Head tag so whatever you want to include send that information before header is sent....!!

and it will be good if you will make a redirect_to() function and there you redirect to any page by using some if else and some indicators...!!

PhpSeeker