0
    <table>
     <tr>
      <td width="30"></td>
      <td><img src="Images/logo.jpg" width="100" height="100" />
      <td><td><h2>&nbsp;&nbsp;&nbsp;&nbsp;Kunal Structures (India) Pvt. Ltd.</h2></td>
     </tr>
    </table>
    <?php

    if(!isset($_SESSION['user_id']))
    {
        header("location:http://127.0.0.1/New%20folder/KSIPL1.10/KSIPL/index.php");
    }
    else 
    {
        $now = time(); // checking the time now when home page starts
        if($now > $_SESSION['expire'])
        {
                header("location: http://127.0.0.1/KSIPL1.10/KSIPL/index.php"); 
                echo "Session Expired !"." <a href='http://127.0.0.1/New%20folder/KSIPL1.10/KSIPL/index.php'>"." <h1> ".
                "Login again"." </h1>"."</a>";

                session_destroy();
        }
        else
        {
            $qry = mysql_query("SELECT * FROM user_main WHERE user_id=".$_SESSION['user_id']."");
            $loguser=mysql_fetch_assoc($qry);

            ?>
                <table class="headerinfo" align="right">
                <tr>
                <td>You are <span style="color:#40B3BA;"><?php echo $loguser['user_name']; ?></span>,</td>
                <td><a style="color:#f5f5f5; text-decoration:none;" href="logout.php"><b>Logout</b></a></td>
                <td>&nbsp;&nbsp;&nbsp;</td>
                </tr>
                </table>
            <br/>
</div>  
            <?php

            if($loguser['user_type'] == "Administrator") //Custom Menubar for Administrator
            {
                ?>
                <div class="row-fluid">
                    <div class="navbar">
                    <div class="navbar-inner">
                        <a class="brand" href="#"></a>
                        <ul class="nav">
                        <li><a href="">Home</a></li>
                        <li><a href="">Documents</a></li>

                        </ul>
                    </div>
                </div>
                </div>
                <?php
            }
        }
    }

why it shows me an error like header already sent and do not redirect to given path. When session_['expire'] condition becomes true it shoud be move to index.php file but it not happen because of the header error. How can i solve this problem. Is there any solution for avoiding header error.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user2881770
  • 45
  • 1
  • 9

4 Answers4

6

Because when you use a php header function, as the name says, it is a header and must be sent before any content.

Whenever you start sending html, you server closes the header section and starts sending the content. Then, a couple of lines later, you ask him to add something to the headers. That's the error 'headers already sent', He cannot add it anymore.

To solve this, move your php before any plain html is written (even a small space is enough to trigger the header end.)

and another detail, it doesn't mae sense to echo something after the header because it will never be shown.

you can replace

header("location: http://127.0.0.1/KSIPL1.10/KSIPL/index.php"); 
echo "Session Expired !"." <a href='http://127.0.0.1/New%20folder/KSIPL1.10/KSIPL/index.php'>"." <h1> ".
                                "Login again"." </h1>"."</a>";

session_destroy();

with

session_destroy();
header("location: http://127.0.0.1/KSIPL1.10/KSIPL/index.php"); 
Vincent Duprez
  • 3,772
  • 8
  • 36
  • 76
1

You have to use the php header function before any output is made. So you will have to restructure some code

<?php
session_start();
if(!isset($_SESSION['user_id'])){
    header("location:http://127.0.0.1/New%20folder/KSIPL1.10/KSIPL/index.php");

}
else{
    $now = time(); // checking the time now when home page starts
    if($now > $_SESSION['expire']) {
        session_destroy();
        header("location: http://127.0.0.1/KSIPL1.10/KSIPL/index.php"); 

    }
}
?>
<html>
<!-- put html code here... ->
daker
  • 3,430
  • 3
  • 41
  • 55
0

If you get a "Headers already sent" error, there are three likely causes.

If this error is not the first error message on the page, then it is most likely a 'avalanche effect' of previous errors and you may ignore it. Instead, focus on fixing the errors before it. When you fix the first error message(s), the "Headers already sent" error(s) will most likely disappear.

This normally can be resolved by a quick custom php.ini alteration:

1) Create a custom php.ini file (if there isn’t one already)

  • For information on how to create a custom php.ini file, please click here.

2) Locate the line that looks like:

output_buffering = Off

3) Change the output_buffering line to:

output_buffering = On

This should be all that is needed to bring the site back online.

Ref: http://kb.site5.com/php/how-to-repair-headers-already-sent-php-errors/

Also you can use to add <?php @ob_start();?> in top of the php file

Krish R
  • 22,583
  • 7
  • 50
  • 59
  • If you want your code to be able to run on other servers, dont do this. I have never encountered a server that had this option on. – Hugo Delsing Dec 12 '13 at 08:06
  • so how i can solve this problem for all the server. – user2881770 Dec 12 '13 at 08:08
  • simply by coding your headers before your content, this answer is correct by kind of keeps all data temporary serverside so it can still affect the headers. In almost any case (like yours), this can be solved by moving code. some rare integration cases server-side-cross-domain this may be helpful. – Vincent Duprez Dec 12 '13 at 08:28
0

i agree with Vincent Duprez but there is the alternative in javaScript

here is the updated code of "daker"

<?php
session_start();
if(!isset($_SESSION['user_id'])){
//header("location:http://127.0.0.1/New%20folder/KSIPL1.10/KSIPL/index.php");?>
<script>
window.location="http://127.0.0.1/New%20folder/KSIPL1.10/KSIPL/index.php";
</script>
<?php
}
else{
$now = time(); // checking the time now when home page starts
if($now > $_SESSION['expire']) {
    //header("location: http://127.0.0.1/KSIPL1.10/KSIPL/index.php"); 
?>
<script>
window.location="http://127.0.0.1/KSIPL1.10/KSIPL/index.php";

</script>
<?php        
session_destroy();

}
}
?>

It will works, kindly run :)

  • This is true only if there is no security issue here! doing redirection server side includes hiding stuff from the user. javascript is just automating user actions browser side. So yes, if and only if you move your users to another page and don't need php session control. – Vincent Duprez Dec 12 '13 at 08:25