0

I am creating an API, using PHP. I am getting the following errors:

Warning: Cannot modify header information - headers already sent by (output started at xxx/api/index.php:1) in xxx/api/index.php on line 2

Warning: Cannot modify header information - headers already sent by (output started at xxx/api/index.php:1) in xxx/api/index.php on line 3

Warning: Cannot modify header information - headers already sent by (output started at xxx/api/index.php:1) in xxx/api/index.php on line 4

Warning: Cannot modify header information - headers already sent by (output started at xxx/api/index.php:1) in xxx/api/index.php on line 5

Warning: Cannot modify header information - headers already sent by (output started at xxx/api/index.php:1) in xxx/api/index.php on line 6

There is not white-space above or below, still I am getting the warnings! Below is my code:

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
echo '{"abc":"abc"}';
?>

Any help will be appreciated!

PS: Why this is being marked as duplicate! I assume the problem is being caused by the headers! and all other questions are of different criteria!

Thanks

Muhammad Abbas
  • 109
  • 4
  • 10
  • 1
    Is this file being included in another file? Are you running a framework? The path `xxx/api/index.php` strongly hints this is part of some framework. – elixenide Apr 21 '18 at 15:58
  • No, this file is not being included! This is not a framework. I have created this path myself! – Muhammad Abbas Apr 23 '18 at 07:21
  • It probably means that there are extra spaces or lines after a closing ?> php tag – Robert Apr 23 '18 at 07:49
  • There are none! No spaces, not extra lines!!! I posted the question, after verifying the spaces and lines! – Muhammad Abbas Apr 23 '18 at 08:56
  • @MuhammadAbbas There are only a couple of possibilities. One is a global include that runs before every PHP file (check your .htaccess and php.ini files). Another possibility is an invisible space in this file before the ` – elixenide Apr 23 '18 at 11:56
  • Strange! I copied all the contents into a new PHP file and it worked!!! – Muhammad Abbas Apr 23 '18 at 15:40

1 Answers1

0

If this is the only file in your app, then try deleting the closing PHP tag (?>). If this is not the only file in your app, then you probably have white space in a different file, perhaps one that is requiring this one.

Keegan Brown
  • 515
  • 3
  • 5
  • Tried deleting ?>, but no success. This is the only file! I am not including this file anywhere! – Muhammad Abbas Apr 23 '18 at 07:20
  • You probably have a character before the opening php tag then. It's possible it was a zero-width character on the first line, or some other kind of un-renderable character, or a tab or something else you're not showing here. Regardless, it sounds like you solved it. The problem you're experiencing usually comes from trying to modify the headers when the php engine has already tried to render any kind of output, which is why any characters outside of the PHP tags are suspect. In order to modify the headers, you have to ensure that PHP has not tried to render anything at all. – Keegan Brown Apr 25 '18 at 16:35