I\'m sure lots of people have had the problem before, the annoying \"Headers already sent by output....\" PHP error.
I\'ve always tried to structure code such that headers are infact the first output on a page, although occasionally its infeasible and impractical in the context of a particular design, for this reason I thought I\'d share a simple workaround, it may not be the most elegant approach, but it saves lots of time, code and possible headaches.
If you had code such as the following:
[code]<?php
echo "foo";
header("Location: http://www.google.com");
?>[/code]
this error would appear, quite rightly, as headers should be the first thing the browser receives in order to render a page correctly, if the browser receives \"foo\" first, well... we don\'t have a good situation.
Suppose that the code could not be changed around, lets say the header\'s location (in this case [url=\"http://www.google.com)\"]http://www.google.com)[/url] wasn\'t actually a string URL, but was instead influenced by a variable set throughout the execution of the script. Something like the following
[code]<?php
$url = $_GET[\'url\'];
header("Location: {$url}");
?>[/code]
I hope to god nobody ever DOES use code like this, but anyway...
This case would call for output buffering (or some other way that I\'m not familiar with, I never claim to be great at PHP <img src=\'http://www.killanet.net/forum3/public/s ... >/blum.gif\' class=\'bbc_emoticon\' alt=\':P\' />)
The following code will suppress any errors, and prioritise the information to be sent, such as the header command, which should be sent first.. will be cached at the beginning of the buffer, and will therefore be output first once the buffer is \"flushed\" if there were any echo or print statements within the code, they would follow the headers in terms of priority.
[code]<?php
ob_start();
$url = $_GET[\'url\'];
header("Location: {$url}");
ob_end_flush();
?>[/code]
Note, if you intend to use output buffering, its generally a good idea to just have all your output inside the buffer, unless you want to get complicated, in which case you can use multiple buffers, but that\'s out of the scope here!... the easiest way to accomplish output buffering is to have ob_start(); as your first line of code (obviously comments can preceed), and ob_end_flush(); as your last.
If there\'s anything wrong here, or if you need help clarifying anything, post back!
Regards
Paul
Output Buffering (Get rid of "headers already sent" errors
Support, Snippets & Projects
Moderators: Moderator, Global Moderator
Output Buffering (Get rid of "headers already sent" errors
[color=\"green\"]'class KPIM::ProcessManager' only defines private constructors and has no friends[/color] <-
poor class
[18:09:00] * ~Moppy stews ner0 erotically 1 times.
[18:09:00] * ~Moppy stews ner0 erotically 1 times.
Jump to
- General Category
- ↳ KillaNet Country
- ↳ 2D Graphics
- ↳ KillaNet News
- ↳ 3D & Animation
- ↳ Chatroom
- ↳ Flash
- ↳ Help & Suggestions
- ↳ Game Dev
- ↳ Audio & Video
- ↳ Introductions
- ↳ Journalism
- ↳ Phoenix Lounge
- ↳ Application Dev
- ↳ Toga Toga Toga!!!
- ↳ Photography
- ↳ Main Street Archives
- ↳ Web Design
- ↳ Fonts Icons Cursors & Screensavers
- ↳ Book Reviews
- ↳ General
- ↳ Tech Industry News
- ↳ Legal Resources
- ↳ Industry Contests
- ↳ Graphix Battle Arena
- ↳ KillaNet Contests
- ↳ Competition Archives
- ↳ Education Information
- ↳ Careers
- ↳ Game Studies
- ↳ Motivation
- ↳ Conferences & Seminars
- ↳ KillaDesign
- ↳ Animation & Film
- ↳ The Studio
- ↳ Game Development
- ↳ 2D Graphics
- ↳ Audio & Video
- ↳ 3D Graphics
- ↳ Flash
- ↳ Fonts, Icons & Emoticons
- ↳ Design Requests
- ↳ PhotoShop
- ↳ Cinema 4D
- ↳ Bryce
- ↳ Flash
- ↳ Paint Shop Pro
- ↳ Blender
- ↳ Poser
- ↳ The Darkroom
- ↳ Photo & Camera Discussion
- ↳ Photo Journalism
- ↳ Web Design Principles
- ↳ PHP & MySQL
- ↳ Designing For Print
- ↳ Writer\'s Desk
- ↳ Fiction Writing
- ↳ News Journalism
- ↳ Poetry
- ↳ Technical Writing
- ↳ Biographical Writing
- ↳ General Writing Resources
- ↳ Promotional Writing
- ↳ Film & Television
- ↳ VideoGames
- ↳ Computer Department
- ↳ General Discussion
- ↳ Windows Help
- ↳ Linux Help
- ↳ Builds & Mods
- ↳ Geek Gadgets
- ↳ Security
- ↳ Dev Discussion
- ↳ C++
- ↳ Visual Basic
- ↳ Java
- ↳ Application Skinning
- ↳ IRC Scripting
- ↳ Gaming Centre
- ↳ Guild Wars
- ↳ Aion
- ↳ Computer
- ↳ World of Warcraft
- ↳ Nintendo
- ↳ General RPG & MMORPG
- ↳ PlayStation
- ↳ XBox
- ↳ Other Consoles
- ↳ All Action
- ↳ Racers
- ↳ Sports
- ↳ FPS
- ↳ RTS
- ↳ Sim
- ↳ Casual Games
- ↳ Kids' Games
- ↳ Mobile Games
- ↳ Retro Games
- ↳ Challenges, Friendly Taunts & Discussion
- ↳ Game Requests & Bug Reports
- ↳ Open Source Games
- ↳ Puzzle Games
- ↳ HeadQuarters
- ↳ Uber Coffee Room
- ↳ KillaNet
- ↳ Coffee Room
- ↳ KillaNet
- ↳ KillaGraphix
- ↳ KillaHosting
- ↳ Marketing etc.
- ↳ Staff Issues
- ↳ Reference & Software
- ↳ Archives
- ↳ KillaBlogs
- ↳ Journalism
- ↳ Trash Can
