Solved

Setcookie after Headers sent

Posted on 2004-10-14
6
234 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 
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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

687 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