Preloading script not working

Hi, i have recently restarted a website that i let languish a few years ago again and got it back online (www.bathingwithtoasters.com), but as you can see at the top there are 2 warnings about the prehead script. I haven’t spoken to the guy who did my coding in a little while, and i know virtually nothing of PHP, so was basically wondering what to do.

I am pretty sure the code concerned codes for a cookie, to remember the images when you mouseover the menu buttons, as they had to reload a lot previously, and it took far too long, but am not certain. The complete code is as follows:

[php]<?php
if (isset($_COOKIE[‘amount’]))
$amount = $_COOKIE[‘amount’];
else $amount = 1;
$page = $_SERVER[‘PHP_SELF’];

if (isset($_COOKIE[‘page’]) && $_COOKIE[‘page’]==$_SERVER[‘PHP_SELF’]) $amount=$_COOKIE[‘amount’]+1;
else $strange=“yes”;

setcookie(‘page’, $page, time()+3600);
setcookie(‘amount’, $amount, time()+3600); ?>[/php]

If i need to post any other code or whatever, jsut say and i will add it. I did recently change hosting so this may also be relevant. I regretfully probably need layman’s terms for any help, as i’m pretty new to PHP, thanks for any help you can offer.

Nick

Warning: Cannot modify header information - headers already sent by (output started at /home2/bathingw/public_html/index.php:2) in /home2/bathingw/public_html/prehead.php on line 10

Warning: Cannot modify header information - headers already sent by (output started at /home2/bathingw/public_html/index.php:2) in /home2/bathingw/public_html/prehead.php on line 11

This error is probably being reported due to the setcookie() call. That function sends a header to the browser, but you can not send a header if you have already sent content. The warning is telling you that the index.php file sent something (other than a header) to the browser before the include (or require) for the prehead.php file. You need to look at index.php and see what is there.

If you can’t figure it out, you can post the start of index.php (up to the include/require) and we can see if we can help.

Hi david, thanks for getting back to me. I have had a look and there isn’t an awful lot in the top of index.php, all i can think is maybe the link, or the title are some how in the wrong place or things are out of order

[php]

<?php require "prehead.php"; ?> Bathing With Toasters <?php require "metadata.php"; ?>
<?php include "1head.php"; ?>
[/php]

And just in case it is needed: metadata.php
[php] [/php]

and the 1head.php code:
[php]

 

<?php include "rthoughts.php"; ?>

[/php]

Sorry for the bombardment of code, and thanks for the help, i really appreciate it.

The DOCTYPE is sent to the browser. You can’t send anything before sending a header (such as a setcookie() or header() call). Try swapping these two lines in index.php from

[php]

<?php require "prehead.php"; ?>[/php]

to

[php]<?php require "prehead.php"; ?>

[/php]

Seems to have fixed it! Thanks David :slight_smile:

On closer inspection this seems to have worked on some pages and not others, which seems odd as they are all made from the same template, for example: http://www.bathingwithtoasters.com/downloads.php, should i just repeat the same change on all those subsequent pages that have this problem?

or better still is there a quick way of doing it to all pages concerned?

You will need to look at each script to see where there is output before any headers are sent. Note that if there is any whitespace before the openning PHP tag (<?php) that whitespace is sent to the browser, so it needs to be removed. Also, if your files are saved as UTF-8 you need to make sure you save them WITHOUT BOM (look at the encoding options in your editor).

If you look closely at the error (warning) message, it tells you the name of the file and the line number where the output started. This is where you need to start looking. Always start with the FIRST error message as subsequent error messages can be caused by the cause of the first message.

Warning: Cannot modify header information - headers already sent by ([b]output started at /home2/bathingw/public_html/index.php:2[/b]) in /home2/bathingw/public_html/prehead.php on line 10

There is another solution to this problem, but I do NOT recommend it. I consider it to be a work-around, not a solution. This “solution” involves output buffering (see ob_start() in the manual). It is important to understand what these functions do and how it impacts your script. Some people advocate just adding ob_start() at the beginning of any script with this problem, but that is just hiding bad programming with more bad programming, and leaves unresolved issues to bite you in the read-end later.

Sponsor our Newsletter | Privacy Policy | Terms of Service