/    Sign up×
Community /Pin to ProfileBookmark

using header() after output was sent?

Hi, is there a way I can user the header() command after I printed something? What I am trying to do is something like:

[CODE]print(“you will be redirected after 3 seconds..”)
flush();sleep(3);
header(“location:page.php”);[/CODE]

but as you can imagine, I get the all known “headers already sent…”.
I know there are alternatives using javascript or meta but I would like to know if it’s possible to do in php

thanks in advance

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@QuidamJun 29.2008 — nevermind
Copy linkTweet thisAlerts:
@biggie_macauthorJun 29.2008 — ? thanks for the suggestion but as I said...

I know there are alternatives using javascript or meta but I would like to know if it's possible to do in php[/QUOTE]
Copy linkTweet thisAlerts:
@NogDogJun 29.2008 — You could send a "Refresh:" header instead of a "Location:" header. It would still have to be executed before any output is sent, though.
[code=php]
header("Refresh: 3;http://www.yoursite.com/page.php");
echo "<html><head><title>Redirecting...</title></head><body>n";
echo "<p>You will be redirected in a few seconds....</p></body></html>";
exit;
[/code]
Copy linkTweet thisAlerts:
@felgallJun 29.2008 — Of course using a refresh header means that not everyone will be redirected since some browsers don't support them and those that do allow them to be turned off.
Copy linkTweet thisAlerts:
@rootJul 01.2008 — header() over here.
Copy linkTweet thisAlerts:
@Phill_PaffordJul 01.2008 — [code=php]
/**
* redirect to a specific URL
* @param $url
*/

function redirect($url)
{
if (!headers_sent())
{

//If headers not sent yet... then do php redirect
header('Location: '.$url); exit;
}
else
{

//If headers are sent... do javascript redirect...
//if javascript disabled, do html redirect.
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>'; exit;
}
}
[/code]
Copy linkTweet thisAlerts:
@felgallJul 01.2008 — Why not simply buffer the page content and write it out after all the headers have been written.
×

Success!

Help @biggie_mac spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...