Paul
Paul

Reputation: 1

PHP redirect with header() is opening redirected page within first page

I am writing a PHP page that contains outputs, and then an if then statement that results in:

ob_start();
header('Location: http://www.mydomain.com/');
ob_end_flush();

There are no errors, but the page it is redirecting to is opening up within the main page. You can see the top half of the main page, then the entire contents of the page it's redirecting to, and then the bottom half of the main page. It all appears on the same screen. This is really causing havoc for my site. How can I just make it simply go to the new page?

Upvotes: 0

Views: 267

Answers (1)

Daniel García
Daniel García

Reputation: 622

It really not make any sense to output the buffer and make a redirect within.

I suggest to make all the checks you need to assure you need to output content or redirect, and then make one of them.

You should also stop the script execution after changing the header location. Something like :

if($needToRedirect){
      header('Location: http://www.mydomain.com/');
      exit();
}else{
      ob_end_flush();
}

Upvotes: 1

Related Questions