justjoe
justjoe

Reputation: 5564

How to redirect if the header is been sent?

I use wordpress and unfortunately, I have to work behind the backend. I need to print message about user action while the CMS header has been sent.

Is there any alternative than using javascript? I prefer a PHP solution.

echo '<script type="text/javascript">/* <![CDATA[ */'; 
echo 'window.location.href = "'. $redirect_url . '";'; 
echo '/* ]]> */</script>';

Upvotes: 1

Views: 147

Answers (3)

KoolKabin
KoolKabin

Reputation: 17653

I prefer:

1.) place config.php to load at the top of every page and put ob_start in it

2.) place end_load.php to load at the end of every page and put the ob_end in it

this way will ensure no output is given to client till the end of processing and you can use header redirect at any time of page.

i am ignorant of disadvantages of it. May be caching has disadvantage but not sure

Upvotes: 0

Mawg
Mawg

Reputation: 40140

You can't undo that header sending ... but you can buffer it, which is what I ended up doing when I asked this question.

Hope that helps

Upvotes: 1

Mike Johnson
Mike Johnson

Reputation: 351

You can pass the message by using query string:

header('location: index.php?msg=1');exit;

And use swich to show the message based on value of msg query string var:

switch($_GET['msg'])
{
   case: 1; // do foo
   case: 2; // do bar
   case else : ; // ooops
}

Upvotes: 1

Related Questions