Solved

Setcookie after Headers sent

Posted on 2004-10-14
6
228 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

910 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now