Ugh... Warning: Cannot modify header information - headers already sent...

7 replies
Wondering if anyone knows anything about the following error:

PHP Code:
Warning:  Cannot modify header information headers already sent  by (output started at  /home/poison1/public_html/wp-content/themes/thesis_17/lib/html/frameworks.php:6)  in /home/poison1/public_html/wp-content/themes/thesis_17/custom/custom_functions.php  on line 38 
Here's my function that's supposedly causing this problem. Line 38 is the setcookie() line.

PHP Code:
function add_cookie() {
if (
$_GET['gclid']) {
$expire=time()+3888000;
setcookie("Believe_Adwords",$_GET['gclid'],$expire,"/",".nickholliday.com");
}

I've busted my tail trying to figure this out. Here's what I've done to try to fix it:

  1. make sure there are no spaces following my final ?>
  2. make sure my file is saved in UTF-8 no MOD
  3. actually typed this function in, rather than cutting & pasting, just to make sure there are no hidden characters in it
I'm stumped!
#header #modify #ugh #warning
  • Profile picture of the author TristanPerry
    Is there a space *before* the first <?

    Have you output anything else? (echos, print, print_all?)
    Signature
    Plagiarism Guard - Protect Against Content Theft
    {{ DiscussionBoard.errors[2357902].message }}
  • Profile picture of the author mywebwork
    Your error may not be in tis section of the code, or even in his script for that matter.

    PHP's "header" function must be the first thing to write HTTP output, if it isn't you'll get exactly this error. So if this script is being called by another script that has already written out a page (or part of one) if will fail in exactly this fashion.

    I suggest you look at the code that calls "add cookie" and make sure it's been called after the header command and not before it.

    Bill
    {{ DiscussionBoard.errors[2358201].message }}
    • Profile picture of the author nmarley
      From the PHP manual:

      setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.
      Found here: PHP: setcookie - Manual


      It looks like you're calling setcookie after the headers have already been passed. I could be wrong (and frequently am), but I believe the cookies are part of the headers, so they should be set first.
      {{ DiscussionBoard.errors[2359071].message }}
      • Profile picture of the author Western Grizzlin'
        Originally Posted by nmarley View Post

        From the PHP manual:

        Found here: PHP: setcookie - Manual


        It looks like you're calling setcookie after the headers have already been passed. I could be wrong (and frequently am), but I believe the cookies are part of the headers, so they should be set first.
        Yes, I think you're right. I'm putting the call to the function inside of <head>.

        Now I just have to figure out how to get this function into the headers in wordpress. I wonder if there is a hook...
        {{ DiscussionBoard.errors[2363177].message }}
        • Profile picture of the author Western Grizzlin'
          Figured it out, all I had to do was change my code from

          PHP Code:
          add_action('wp_head','add_cookie'); 
          to

          PHP Code:
          add_action('send_headers','add_cookie'); 
          It feels good. Thanks so much everyone for your help!
          {{ DiscussionBoard.errors[2363186].message }}
  • Profile picture of the author n7 Studios
    A quick fix would be to add ob_start() to the first line of your page's PHP code.

    I say quick fix, as this may cause other issues, so it's worth following some of the above suggestions in terms of setting your cookies before outputting data to the web page.
    {{ DiscussionBoard.errors[2359816].message }}
  • Profile picture of the author mihir
    I had encountered this type of problem. This can occure if there is some meta information is added into your file. Like you created a text file first n renamed to php. To resolve this issue, copy content of your file, open notepad, paste content n then save file "filename.php" with quotes.

    This should solve your problem.
    Signature
    HostCP - cPanel iPhone app to manage your cPanel accounts using cPanel's official API
    WordpressInstaller.net - Automatically install & configure Wordpress Blog, Theme, Plugins and Posts. Wordpress Installer will DO IT ALL for you in just a FEW SECONDS AND best of all, it's FREE
    Mihir.info - A developer's blog
    {{ DiscussionBoard.errors[2403093].message }}

Trending Topics