Staredit Network

Staredit Network -> Computers and Technical -> [PHP] Cookie after content?
Report, edit, etc...Posted by Pie_Sniper on 2006-07-28 at 21:42:24
I believe I am correct in saying that cookies can only be set before any content has been sent to the browser. But, what if we need to save some configuration when they leave or refresh? Here's my code...
CODE
// Reading in cookie at the beginning and setting some values
 if( $_COOKIE['sbox'] == "no" )
 {
   $Image = "Images/Show.gif";
   $Style = "display: none;";
 }
 else
 {
   $Image = "Images/Hide.gif";
   $Style = "display: block;";
 }

// Setting the cookie
   <script language="javascript" type="text/javascript">
     // <!--
     ...
     function SaveCookie()
     {
       var Image = document.getElementById( "ToggleImage" ).src;
       if( Image == "http://mould.dyndns.org/Images/Hide.gif" )
       {
         <?php setcookie( "sbox", "yes" ); ?>
       }
       else if( Image == "http://mould.dyndns.org/Images/Show.gif" )
       {
         <?php setcookie( "sbox", "no" ); ?>
       }
     }
     ...
     // -->
   </script>

// Calling the function
 <body onload="SetColor();" onunload="SaveCookie();">
   ...

Now, of course this doesn't work, because when I set the cookie, I've already sent a bunch of stuff. So, what should I do? I thought of having it redirect to a page in SaveCookie(); with a GET and then redirecting back, but then they couldn't leave the site, and I don't even know if it will allow a redirect like that (onunload).

:: Edit
I looked around and it said that changing "output_buffering" in php.ini to "On" would let you do this, but when I did so and restarted Apache, I had the same result.
Next Page (1)