1

Here is my code:

<?php 
session_start();

No other includes, spaces or anything. The entire file has two lines.

The output I get:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\webpage\test.php:1) in C:\xampp\htdocs\webpage\test.php on line 2

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\webpage\test.php:1) in C:\xampp\htdocs\webpage\test.php on line 2

I am using Xampp. Any ideas?

EDIT: From php.ini

session.save_handler=files
session.save_path="C:\xampp\tmp" (which exists and is writable by everyone)
Jay Blanchard
  • 32,731
  • 15
  • 70
  • 112
Jacques
  • 1,529
  • 1
  • 12
  • 22

4 Answers4

2

As per my comment (asking what the encoding was), it's an encoding issue (as per what you said also).

If UTF-8 is the format you must use, then you need to save that file without the BOM (byte order mark).

It counts as output.

More on that can be found here:

Read the following on Stack in regards to the differences between with and without BOM:

"@Fred it seems it was encoding. Weird thing is I am am using PHPStorm. Shouldn;t the encoding always be UTF8? – Jacques"

Not always. Certain applications require a UTF-8 format be used (databases, accents on characters, etc.). You can use any format you want, just as long as it works for the way it is to be used.

If you don't need to use sessions, then don't add session_start();.

Here is an article on the W3C website that you can read:

and one here on Stack:

and if you have time:

You can further your research by using "why should I use utf-8?" as keywords in your favorite search engine.

Community
  • 1
  • 1
Funk Forty Niner
  • 73,764
  • 15
  • 63
  • 131
1

Check either session was started or not.

Recommended way for versions of PHP >= 5.4.0

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

Source: http://www.php.net/manual/en/function.session-status.php

For versions of PHP < 5.4.0

if(session_id() == '') {
    session_start();
}
Nere
  • 3,919
  • 4
  • 24
  • 62
0

Add a line after the opening PHP tag:

<?php

ob_start(); //Add this!
session_start();
Anders
  • 7,431
  • 6
  • 42
  • 76
ImBhavin95
  • 1,403
  • 2
  • 14
  • 25
0

Besides what others say, you can also enable output buffering in your php.ini. If you set for instance:

output_buffering = 4096

Then all your output will be buffered (up to 4k) before being sent to browser. This will prevent any accidental output to be sent before your session_start().

Jack
  • 1,210
  • 7
  • 15