douqian8238 2014-04-03 09:36
浏览 35
已采纳

PHP Cookie错误 - 标题已发送[重复]

This question already has an answer here:

Could somebody tell me what's wrong? in the following code? I get the error Cannot modify header information - headers already sent.

<?php
class Cookie{

    public static function exists($name){
        return (isset($_COOKIE[$name])) ? true : false;
    }

    // Get the value of a cookie
    public static function get($name){
        return $_COOKIE[$name];
    }

    // Create a cookie
    public static function put($name, $value, $expiry){

        if(setcookie($name, $value, time() + $expiry, '/')){
            return true;
        }
        return false;
    }

    // Delete Cookie
    public static function delete($name){
        self::put($name, '', time() -1);
    }
}
?>

The error is from the above code I guess. I'm also including my Logout page since the error above is given when this code bellow is processed.

<?php
    require_once ('functions/sanitize.php');
    require_once('core/init.php');
    include('includes/header.php');

    $user = new User();
    $user->logout();
 ?>

Bellow is the call stack

1 0.0002 237176 {main}( ) ../logout.php:0

2 0.0051 356400 User->logout( ) ../logout.php:7

3 0.0054 351736 Cookie::delete( ) ../User.php:125

4 0.0054 351864 Cookie::put( ) ../Cookie.php:24

5 0.0054 352008 setcookie ( ) ../Cookie.php:16

setCookie() thats where the error is

Anyways this is my header.php file which is included

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?php echo $title ?></title>

     <!-- Bootstrap -->
    <link href="../css/bootstrap.min.css" rel="stylesheet">
    <link href="../css/styles.css" rel="stylesheet">
    <link rel="stylesheet" href="../css/image-hover.css" type="text/css">

    <!--[if IE 7]><link rel="stylesheet" href="ie7only.css" type="text/css" /><![endif]-->
    <!--[if IE 8]><link rel="stylesheet" href="ie8only.css" type="text/css" /><![endif]-->
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <link href='http://fonts.googleapis.com/css?family=Nunito:400,700,300' rel='stylesheet' type='text/css'>
    <script type="text/javascript" src='../jQuery/jquery-1.9.1.min.js'></script>
    <script src="../jQuery/jquery.easing.1.3.js" type="text/javascript"></script>

    <!-- Scripts
    ================================================== -->


    <!--[if lt IE 9]>
        <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
  </head>
  <body>
<div class="row">
    <div class="navbar navbar-default navbar-static-top">
        <div class="navbar-header">
          <a href="../" class="navbar-brand">WebA<font style="color: #65bd3f;"><strong>ww</strong></font>ards</a>
          <button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#navbar-main">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
        </div>
        <div class="navbar-collapse collapse" id="navbar-main">
          <ul class="nav navbar-nav">
            <li class="dropdown">
              <a class="dropdown-toggle" data-toggle="dropdown" href="#" id="themes">CATEGORIES <span class="caret"></span></a>
              <ul class="dropdown-menu" aria-labelledby="themes">
                <li><a href="blogs">BLOGS</a></li>
                <li><a href="ecommerce">ECOMMERCE</a></li>
                <li><a href="corporate">CORPORATE</a></li>
                <li><a href="portfolio">PORTFOLIO</a></li>
                <li><a href="minimal">MINIMAL</a></li>
              </ul>
              </li>
            <?php $user = new User(); if(!$user->isLoggedIn()){
            echo '<li>';
              echo '<a href="../login">REGISTER/ LOGIN</a>';
            echo '</li>';
            } ?>
            <li>
              <a href="../search/index">SEARCH</a>
            </li>
          </ul>
          <ul class="nav navbar-nav navbar-right">
            <?php $user = new User(); if($user->isLoggedIn()) { ?>
             <li class="dropdown">
              <a class="dropdown-toggle" data-toggle="dropdown" href="#" id="themes">
                <?php echo 'Hello, ', escape($user->data()->Username), ' !'; ?>
                <span class="caret"></span></a>
              <ul class="dropdown-menu" aria-labelledby="themes">
                <li><a href="../update">Update Profile</a></li>
                <li><a href="../logout">Logout</a></li>
              </ul>
            </li>
            <?php } ?>
            <li class="web"><a href="../submit"><font style="color: #ffffff;"><strong>SUBMIT YOUR WEBSITE</strong></font></a></li>
          </ul>
        </div>
    </div>
</div>
</div>
  • 写回答

1条回答 默认 最新

  • dsigh7478 2014-04-03 09:54
    关注

    Your structure should be next :

    <?php
        require_once ('functions/sanitize.php');
        require_once('core/init.php');
    
        $user = new User();
        $user->logout();
        // and all others operations with headers
    
        include('includes/header.php');
        // and all others html to output
    
     ?>
    

    When you send html to client you send headers with it. You can not modify headers after you send them to the client (or Headers already sent error appears). You have to do all operations with headers before you send them.

    P.S.: ofc functions/sanitize.php and core/init.php mustn't have any html outputs

    I hope this will help.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效