如何修复PHP中的“Headers already sent”错误,setcookies

时间:2014-09-06 19:27:29

标签: php header output

当我设置cookie或尝试使用标题重定向(“位置...”)时出现错误,任何人都知道如何在不删除setcookie的情况下修复它?我也尝试回应除PHP代码之外的所有内容,但它没有帮助,它只是将第28行中的第一个错误移动到48(在第一个html之后)。

<?php session_start(); 

    if(isset($_COOKIE['username'])){
        $_SESSION['username'] = $_COOKIE['username'];
    }
?>

<!DOCTYPE HTML>

<html>
<head>
    <title> :: BG :: </title>
    <meta charset="utf-8">


    <link rel="icon" type="image/jpg" href="slike/beograd-grb.png"/>
    <link rel="stylesheet" type="text/css" href="stil/loginU.css" />


    </head>
    <body>
        <div id="sadrzaj">
            <div id="gornji_deo">
                <div id="gornji_deo_levo">
                </div>
                <div id="gornji_deo_sredina">
                </div>
                <div id="gornji_deo_desno">
                    <div id="menu">
                        <div id="header1" style="border-top-left-radius:20px"> <a href="index.php">Pocetna</a>
                        </div>
                        <div id="header1"> <a href="formular.php">Formular</a>
                        </div>
                        <div id="header1"> <a href="login.php">Login/Registration</a>
                        </div>
                        <div id="header1"style="border-top-right-radius:20px"> <a href="kontakt.php">Kontakt</a>
                        </div>
                        </div>
                    <div id="slike">
                     <img src="slike/engleska.png"/></br>
                     <img src="slike/srbija.gif"/>
                    </div>
                </div>
            </div>
            <div id="sredina">
                <div id="levi_deo">
                    <div id="levi_deo_tekst">
                    <?php
                include 'db.inc.php';
                //error_reporting(0);

                $username = $_POST['username'];
                $password = $_POST['pass'];



                $password = md5($password);

                connect();
                $upit = "SELECT password FROM korisnici WHERE username='$username'";
                $rezultat = mysqli_query($link, $upit) or die("Greska pri upitu: " . mysqli_error($link));

                $lozinkaBaza=mysqli_fetch_row($rezultat);
                $lozinkaBaza1 = $lozinkaBaza[0];

                if($lozinkaBaza1=='')
                    die('Nije moguce izvrsiti. Korisnik ne postoji ' . mysqli_error($link));
                else{
                    if($lozinkaBaza1==$password){
                        echo "Uspesno ste se ulogovali $username <br/>";
                        echo "<a href='index.php'>Vratite se na pocetnu stranu</a>";
                        $_SESSION['username']=$username;
                        $_SESSION['password']=$password;
                        if(isset($_POST['kolacic'])){
                        setcookie('username', $username, time()+3600*24,"/");
                    }
                    header("Location: index.php");
                    }
                    else{
                        echo "Greska. Pogresan username ili password.";
                    }
                }
                 mysqli_close($link);

                ?>
                </div>
                </div>

                <div id="desni_deo">
                        <?php
                    error_reporting(0);
                    $pom=$_SESSION['username'];
                    if($pom!=null)
                        echo "Dobrodosli $pom!";
                    else
                        echo "Dobrodosli gost!";
                    ?>
                </div>

            </div>
            <div id="donji_deo">
                <div id="copyright">Copyright © Aleksa. All rights reserved. </div>
            </div>

        </div>

    </body>
</html>

错误:

Warning: Cannot modify header information - headers already sent by (output started at /home/ai12257/public_html/loginU.php:28) in /home/ai12257/public_html/loginU.php on line 76

Warning: Cannot modify header information - headers already sent by (output started at /home/ai12257/public_html/loginU.php:28) in /home/ai12257/public_html/loginU.php on line 78

第28行是<div id="gornji_deo_desno">

第76行是setcookie ..

非常感谢:)

2 个答案:

答案 0 :(得分:0)

这实际上不是错误,您必须在关闭PHP标记(?&gt;)或echo命令之前使用所有header()和setcookie()。

但是你可以通过在文件的开头使用ob_start()和在底部使用ob_end_flush()来绕过这个。这将存储所有输出,因此在执行ob_end_flush()

之前不会发送任何内容

答案 1 :(得分:0)

在session_start()之前使用ob_start()函数,在header之后使用exit()函数('location:xyp.php')

喜欢