Solved

Setcookie after Headers sent

Posted on 2004-10-14
6
227 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Author Comment

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

Expert Comment

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

|)iablo
0
 
LVL 1

Expert Comment

by:winmeister
Comment Utility
Sometimes behind huge problems, simple things are hidden!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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.

771 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

11 Experts available now in Live!

Get 1:1 Help Now