4

I am getting an error which is very common.

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/sabarspinmatic/public_html/testing/index.php:1) in /home/sabarspinmatic/public_html/testing/index.php on line 1

But the strange this is I have only one php file with only 1 line of session_start().

PHP Code is:

<? session_start() ?>

Could anyone know why I am getting this error. I checked the source code and its showing <br />. I don't know why its showing this tag.

URL to check the error is: http://www.sabarspinmatic.com/testing/

//PART 2 I am just using single file index.php without use of .htaccess and only l line of code. I tried by putting php at start of tag but it doesn't work.

Hiren Joshi
  • 41
  • 1
  • 3
  • Please show the entire code for the page you link to, what you have above is not it. i get two errors from one line of code. – Toby Allen Feb 17 '16 at 07:46
  • Don't you use URL rewrite or something similar? Seems like the code you shared is not the one which is executed indeed. – Anton Feb 17 '16 at 07:52
  • Your header has already been sent, thus you are not able to make modifications to them with a session_start(). Is the index.php ONLY containing session_start() AND it must be the VERY first thing you call .. I assume this is not used as part of an include() somewhere .. ? – DTH Feb 17 '16 at 07:53
  • Also make sure that there is no BOM (Byte Order Mark) prior to the start of the file. So if your file has been saved as UTF-8 (which has BOM) change it to ANSI .. BOM would be send before your session_start and you cannot see it when you open your file in an editor .. (See a previous post of mine:http://stackoverflow.com/questions/32603736/extra-undefined-character-in-the-beginning-of-response/32608075#32608075) – DTH Feb 17 '16 at 07:58
  • Everything you need to now about your problem and the causes: http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php .. Without more information from you, I'm afraid that is all you can get. – DTH Feb 17 '16 at 08:05
  • No, I haven't included anything else. The only line of code is Nothing else there in the file. – Hiren Joshi Feb 22 '16 at 09:27

5 Answers5

1

First, I would suggest you to use proper starting tag <?php, just in sake of code readability and portability - not all web servers are configured to support the shorter version <?.

Then make sure there is not even a whitespace before the <?php session_start() .... Also some editors (like Windows Notepad) tend to insert some invisible garbage at the very beginning of the file - so I suggest you to open the file in a HEXADECIMAL mode to see if there are really no characters before the <?php .... There could be for example the invisible UTF-8 BOM character.

Last, but not least, use a semicolon ; at the end of the command - no matter it is the only command in the code block, it is simply a best practice.

Anton
  • 2,458
  • 2
  • 18
  • 30
0

Firstly short tags have been deprecated by php

use

<?php   ?>

everywhere instead of

<?  ?>

Also is this your entire page? Do you have a space or a line before very first text in the file

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
0

There me 2 reasons for the ocurrance of the above issue. 1) May in the including files session_start() is already written. 2) May be u are using the session_start() tag in middle of the code.It should be written in the first line of page.

  • as you can see from the error message, it is line 1 - it is neither of the cases you described. – Anton Feb 17 '16 at 07:46
0

From my experience,removing all spaces in your code solves the problem.Especially if you have a white space before

This will cause the error:

..whitespace..<?php

Removing the whitespace solves the problem

0

I had the same problem... It was because of the Byte Order Mark (BOM) EFBBBF at the start of the file. Use any hex editor to remove these chars from the file and it should work.

Vijay
  • 1
  • 1