0

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>
Tauciokas
  • 101
  • 1
  • 1
  • 13

1 Answers1

1

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.

Sharikov Vladislav
  • 7,049
  • 9
  • 50
  • 87
  • 1
    Your code is not rly bad (not looks like newbee - u use classes for example, you try to not combine html and php). I think you have to learn the basics (how the client interact with the server etc). It's not hard at all and will not take much time. When you do this, you will not allow such simple mistakes. – Sharikov Vladislav Apr 03 '14 at 10:04