0

I am using PHP 5.6.

Recently, I have moved my projects to a new server and since then I am facing a strange problem.

The redirects using php header('Location:some_url') is not working if there is a space or tab is there before opening of php tag (<?php) or after closing php tag (?>) into a php file on which user is being redirected. It returns blank page.

It happens in only files which contains pure php codes, no javascript, jquery, or html there in the file.

Sometimes the issues is like, suppose there is a file a.php e.g.

<?php

// some statements here and end of php tag

?>

<?php

// opening of php tag and statements here and end of php tag

?>

Now, technically it should work but it is not working unless I remove the closing and opening php tag from the middle of the file. There are several files are there containing such a codes. Please let me know how to fix it?

Dev
  • 6,570
  • 10
  • 66
  • 112

1 Answers1

0

Either remove the spaces or enable output buffering. This happens because PHP prints everything not within <?php ?> tags literally. To do that, it has to first send all the headers, and then switch to sending body. But the way HTTP protocol is built, headers must always come before the body, so php can't go back and send more headers after the body has started.

Vilx-
  • 104,512
  • 87
  • 279
  • 422