Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
[TIP] How to redirect avoiding headers already sent error
Old 12-23-2007, 01:40 PM [TIP] How to redirect avoiding headers already sent error
J0ker's Avatar
Skilled Talker

Posts: 61
Trades: 0
You need to redirect somewhere in your script but it has to be after the headers have been sent, happens all the time right? Use a javascript redirect.

Just put this wherever the redirect needs to be, such as in a IF statement, etc:
PHP Code:
?>
<script type="text/javascript">
window.location.href='URLHERE';
</script>
<?php
__________________

Please login or register to view this content. Registration is FREE
- Free Gaming Forum/Arcade

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
J0ker is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-23-2007, 05:22 PM Re: [TIP] How to redirect avoiding headers already sent error
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
The only problem with this is if the user has Javascript turned off

Useful mind
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 12-23-2007, 06:43 PM Re: [TIP] How to redirect avoiding headers already sent error
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Another solution, using only php, would be to use output bufferts (functions starting with ob_, such as ob_start()), where all contents is stored as a buffert instead of being sent to the browser. Then you can still use functions such as header(). If no redirect is nessesary you simply retrieve the buffert and output the contents to the browser.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 12-23-2007, 11:43 PM Re: [TIP] How to redirect avoiding headers already sent error
J0ker's Avatar
Skilled Talker

Posts: 61
Trades: 0
Thats cool, can you give me a quick example?
__________________

Please login or register to view this content. Registration is FREE
- Free Gaming Forum/Arcade

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
J0ker is offline
Reply With Quote
View Public Profile
 
Old 12-24-2007, 05:58 AM Re: [TIP] How to redirect avoiding headers already sent error
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
PHP Code:
<?php

ob_start
();

?>
<html>
<head>
<title>My Website</title>
</head>
<body>
Some content...
</body>
</html>
<?php

if(noErrors()) {
    
ob_end_flush(); // 'Flushes' the buffer (outputs to browser)
} else {
    
header("Location: error.php");
}

?>
You can also call a callback function before flushing, which lets you edit the content before outputting it. Here's an example from directly from php.net
(you can read more about it there).

PHP Code:
  <?php

function callback($buffer)
{
  
// replace all the apples with oranges
  
return (str_replace("apples""oranges"$buffer));
}

ob_start("callback");

?>
<html>
<body>
<p>It's like comparing apples to oranges.</p>
</body>
</html>
<?php

ob_end_flush
();

?>
</span> </span> </div> </div>
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 12-24-2007, 01:20 PM Re: [TIP] How to redirect avoiding headers already sent error
J0ker's Avatar
Skilled Talker

Posts: 61
Trades: 0
I see but that can't work in my case because I am using a template system and the headers start in the header.tpl so any thing in the php files will always be after that.
__________________

Please login or register to view this content. Registration is FREE
- Free Gaming Forum/Arcade

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
J0ker is offline
Reply With Quote
View Public Profile
 
Old 12-24-2007, 08:27 PM Re: [TIP] How to redirect avoiding headers already sent error
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Then just put
PHP Code:
<?php ob_start(); ?>
first in your header file, or before you include the header file.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 12-25-2007, 06:17 PM Re: [TIP] How to redirect avoiding headers already sent error
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
Wow, that is a good way But in general I think it's a good rule of thumb to not send any html before you know what to send to a page

That ob thing is nice!

PHP Code:
<?php 
ob_start
(); 

// Code here \\

ob_end_flush();
?>
You could also use a meta refresh, but only in the <head> tags.
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Reply     « Reply to [TIP] How to redirect avoiding headers already sent error
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.26579 seconds with 12 queries