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.

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

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?