tags:

views:

382

answers:

5

I'm a PHP virgin (first day), so please type slowly.

I have a number of images, bg_001.jpg, bg_002.jpg, etc., which I want to rotate through each time the page is refreshed. I tried this:

if (isset($_COOKIE["bg1"])) {
  $img_no = $_COOKIE["bg1"] + 1;
} else {
  $img_no = 1;
}
$filename = 'bg_' . sprintf("%03d", $img_no) . '.jpg';
if (!file_exists("/img/" . $filename)) {
  $img_no = 1;
  $filename = 'bg_' . sprintf("%03d", $img_no) . '.jpg';
}
setcookie("bg1", $img_no, time() + 86400);
print '<img src="img/' . $filename . '" alt="" height="175" width="800"> ';

Instead of a cookie I get a

Warning: Cannot modify header information - headers already sent by (output 
started at /home2/.../about.php:7) in /home2/.../about.php on line 31

Line 31 being the line with the setcookie. I already found pointers about PHP having trouble with Unicode's BOM, but I have no idea how to fix it (if it is the problem here in the first place). So, to make it official (and avoid a "not a real question" label), how do I fix this? :-)

Constructive criticism on my code is welcome too.

epilogue:
Seemed like a common newbie error: several answers toward the same solution within fifteen minutes. Thanks guyz/galz.

So I moved everything except the print to the start of the file, and indeed: fixed.

+3  A: 

With HTTP your header (request/response info) and Content (actual textor binary content) are set separately and Header must precede the Content.

Setting a cookie actually adds a command to the header response, so any modifications to the cookie need to happen before you start outputting any content.

Move all your code that references your cookie before the opening html element on your page and you should be ok.

tschaible
+6  A: 

cookies can only be sent, if there are no information sent prior. This means that the first thing in your PHP file has to be <?php, and nothing can get before that part (not even the dreaded UTF-8 BOM), because if something is before the first <?php (like <html>), then php will send those data for the browser and after data is sent you can't use setcookie. Refactor your code to look something like this:

<?php
 (...)
 setcookie(...)
 (...)
?>
<HTML>
(...)
</HTML>

And double check that editors like notepad didn't put any UTF-8 BOM signatures before the first <?php in the file.

SztupY
+1  A: 

You might also find it easier to use sessions instead of manipulating the cookies themselves. the session_start() still needs to come before any other content but it makes it easier to store data structures, etc.

have a look at http://php.net/session_start

Cfreak
A: 

To get around this without major code changes, use Output Buffering like so,--

<?php
  ob_start();
  // ... my code here
?>
<html></html>
<?php
  // ... end of the file
  ob_end_flush();
?>
The Wicked Flea
A: 

The error/warning you got says it all: headers are already sent

This means that output has been already sent to the browser before the setcookie() method was called.

As you can understand cookies "should be set" before any output is send to the browser.

So check line 7 at about.php. You should have html code there or you might have a call to print or echo.

andreas