PHP Cookie错误 - 已发送标头

时间:2014-04-03 09:36:05

标签: php cookies

有人能告诉我什么是错的吗?在以下代码中?我收到错误无法修改标题信息 - 标题已经发送。

<?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);
    }
}
?>

我猜错误来自上面的代码。我还包括我的Logout页面,因为上面的错误是在处理此代码时给出的。

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

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

Bellow是调用堆栈

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

2 0.0051 356400 User-&gt; 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(),其中错误是

无论如何这是我的header.php文件,包括

<!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>

1 个答案:

答案 0 :(得分:1)

你的结构应该是下一个:

<?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

 ?>

当您向客户端发送html时,您会发送标题。将邮件发送到客户端后,您无法修改标头(或出现Headers already sent错误)。在发送标题之前,您必须对标题执行所有操作。

P.S。:ofc functions/sanitize.phpcore/init.php不得有任何html输出

我希望这会有所帮助。