Solved

Setcookie after Headers sent

Posted on 2004-10-14
6
230 Views
Last Modified: 2008-03-17
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
Comment
Question by:thecode101
  • 3
  • 2
6 Comments
 
LVL 27

Accepted Solution

by:
Diablo84 earned 200 total points
ID: 12310370
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
 
LVL 1

Assisted Solution

by:winmeister
winmeister earned 50 total points
ID: 12310376
Just put ob_start at the VERY BEGINNING of your page. It MUST be the first line.

<?php
ob_start();
?>
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12310386
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 3

Author Comment

by:thecode101
ID: 12310446
Wow, didn't realize it was just that simple. Thanks =)
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12310469
no problem, i was certainly more simple then your last php question :p

|)iablo
0
 
LVL 1

Expert Comment

by:winmeister
ID: 12310506
Sometimes behind huge problems, simple things are hidden!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Scope of $_SESSION 17 39
Really simple no curl. Send a post 3 times 4 25
Cookie not unsetting 7 19
Creating Functions in phpMyAdmin 8 16
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question