-1

I've read 4 or 5 different threads about this so far, but after trying the suggestions, I can't figure this out.

I'm just trying to set a cookie, but on 3 of 5 pages the "Cannot modify" error is showing up. My page still loads though.

This code is in an include on all pages, and it loads before the html on all pages. (it's included in my header.php file, which is on all pages.)

<?php
if (!isset($_COOKIE['loggedinGaArms'])){
    header("location:index.php");
    exit();
}
else{
    $seconds = 2628000 + time();
    setcookie(loggedinGaArms, date("F js - g:i a"), $seconds);
}
/**
 * Check Logged-in Status via Cookie
 */
?>

My Header.php file (just the first few lines)

<?php
include_once "cookieset.php";/******** SET COOKIE INCLUDE ***/
$page = basename($_SERVER['PHP_SELF']);
?>
<!doctype html><!-------------- FIRST LINE OF HTML ----------->
<html>
<head>
    <meta name="robots" content="noindex">
    <meta charset="UTF-8">
    <title>Georgia Arms Inventory</title>
    <link rel="stylesheet" type="text/css" href="_/style.css">
    <script type="text/javascript" src="_/custom.js"></script>
</head>
<body>
    <!--  Start Main Content  -->
    <div id="content">

Both my database and html are UTF-8.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Jacques ジャック
  • 3,682
  • 2
  • 20
  • 43

4 Answers4

2

Given that you has explicitly stated that your are encoding your files as UTF-8 what you may be seeing is the results of having a BOM (byte order mark) at the beginning of your PHP document. This constitutes output to the browser that is generally invisible in an IDE but will cause the failure that you are seeing.

Other helpful tips are to drop the tailing ?> mark from any PHP file that only contains code. It is unnecessary and opens the door for hard to detect white-space to exist after it.

With error reporting turned on the error should give a pretty clear indication of where output was started that caused the header call to fail, so I would suggest cranking up error reporting to see where the true problem actually lies.

Orangepill
  • 24,500
  • 3
  • 42
  • 63
1

The error Cannot modify header information - headers already sent error means that you echo content before you modify the headers. The data that is sent before this error occurs should appear on the web page before the error is printed. The error is fixed if you move the setcookie() function before that content.

Sumurai8
  • 20,333
  • 11
  • 66
  • 100
  • I appreciate the answer, but the `setcookie()` comes before everything that's not php. It's inside an `if` statement. But as you can see from the code I posted, the file is included before the `` tag. – Jacques ジャック Sep 14 '13 at 06:06
  • Look at the actual html it outputs, not the php file. (e.g. `view-source:http://www.yoursite.com/yourpage`). There is simply no other case where this error boils up. – Sumurai8 Sep 14 '13 at 06:08
  • This is what was retured after the PHP error statements ` Georgia Arms Inventory

    Add New Product

    `
    – Jacques ジャック Sep 14 '13 at 06:15
  • I agree that there is no other case, but I'm reusing code that works on 3 other sites for this :/ It's very frustrating and I've been researching and trying to fix this for 4 hours. – Jacques ジャック Sep 14 '13 at 06:17
  • It is only relevant what is returned before that error statement. The error is printed as soon as php reaches the `setcookie()` function. Anything that is printed before that error is causing the error to appear. What @Orangepill says is true: You could simply be printing whitespace/newlines outside php tags. – Sumurai8 Sep 14 '13 at 06:21
  • view-source:http://lucienconsulting.com/Georgia-Arms/categories.php – Jacques ジャック Sep 14 '13 at 06:21
  • What is on line 1 of `categories.php`? Notepad displays a space before the error, so I assume you have a space somewhere there. – Sumurai8 Sep 14 '13 at 06:37
  • There was a space there. There is not anymore, and it's still throwing the error. – Jacques ジャック Sep 14 '13 at 06:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37379/discussion-between-sumurai8-and-jack) – Sumurai8 Sep 14 '13 at 06:48
0

It could be an issue with the file you are including, or any other files that are being used.

Make sure that such files do not have a PHP closing tag ?> at the end (that is, if they only perform logic, and do not output any HTML.

Make sure that there are no accidental spaces before the opening tags <?php on any of the files.

Otherwise, consider using ob_start and ob_get_clean: http://php.net/manual/en/function.ob-start.php

Mike Rockétt
  • 8,947
  • 4
  • 45
  • 81
0

Generally this issue is generated whenever you have a lot of blank spaces or comments in your php file or you may not have end ?> php tag. check this that is there any blank space before opening <?php.

Besides this you may also use ob_start at the start of your php file.

You may take help of

http://php.net/manual/en/function.ob-start.php

Rohitashv Singhal
  • 4,517
  • 13
  • 57
  • 105