• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Setcookie after Headers sent

I would like to know is it possible to use setcookie after headers have been set to the page. If so, please show me how. I read something about output buffering, but I don't quite understand it.
0
thecode101
Asked:
thecode101
  • 3
  • 2
2 Solutions
 
Diablo84Commented:
You would do it something like this:

<?php
ob_start();
echo "Hello World"; //output started so headers would normally already be sent

setcookie ("name","value",time()+3600);

ob_end_flush();
?>

The output is stored in an internal buffer with output buffering so that and data will not be outputted until ob_end_flush is called therefore you can set headers (and thus cookies) after echo'ing or printing content as shown above.
0
 
winmeisterCommented:
Just put ob_start at the VERY BEGINNING of your page. It MUST be the first line.

<?php
ob_start();
?>
0
 
Diablo84Commented:
Theres a bit more indepth information about how it works in the manual:

ob_start: http://www.php.net/manual/en/function.ob-start.php
ob_end_flush: http://www.php.net/manual/en/function.ob-end-flush.php

and also the output control functions page:

http://www.php.net/manual/en/ref.outcontrol.php
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
thecode101Author Commented:
Wow, didn't realize it was just that simple. Thanks =)
0
 
Diablo84Commented:
no problem, i was certainly more simple then your last php question :p

|)iablo
0
 
winmeisterCommented:
Sometimes behind huge problems, simple things are hidden!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now