-2

I have 3 type of forms on my web site and all worked fine until one day. I don't know what happened but I'm getting session errors; by this I mean I get an error whenever I have a PHP script that include the session tag inside it for example this is on my start over the <html> tag:

<?php

// Make the page validate
ini_set('session.use_trans_sid', '0');

// Create a random string, leaving out 'o' to avoid confusion with '0'
$char = strtoupper(substr(str_shuffle('abcdefghjkmnpqrstuvwxyz'), 0, 4));

// Concatenate the random string onto the random numbers
// The font 'Anorexia' doesn't have a character for '8', so the numbers will only go up to 7
// '0' is left out to avoid confusion with 'O'
$str = rand(1, 7) . rand(1, 7) . $char;

// Begin the session
session_start();

// Set the session contents
$_SESSION['captcha_id'] = $str;

?>

When I test this on my website I get:

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home4/domain/public_html/adres/contact.php:2) in /home4/domain/public_html/adres/contact.php on line 16

Exactly the part where the session_start() is ? The same code structure worked before - what happened?

halfer
  • 19,824
  • 17
  • 99
  • 186
New Logo
  • 85
  • 8
  • 1
    The error tells you that `output [was] started at /home4/domain/public_html/adres/contact.php` on line 2. I would recommend running your file through something that will show you the raw bytes, for example the *nix tool `xxd(1)`; you probably have non-printable characters at the head of the document. – TML Oct 03 '14 at 21:45

2 Answers2

2

Check if you have any spaces or newlines before your opening <?php tag.

danmullen
  • 2,556
  • 3
  • 20
  • 28
  • There is a view here that very brief (one sentence) answers are usually better as comments. – halfer Oct 03 '14 at 21:47
  • OK, thanks. I thought that, in this instance, my answer is the usual solution to this type of problem. – danmullen Oct 03 '14 at 21:50
  • Commenting would be the usual approach to deliver answers to duplicate questions, I should think. I'm not downvoting, but short answers are at risk of it. – halfer Oct 03 '14 at 21:51
  • 1
    short but right [halfer] for someone like me who just started with php this short , help's a lot , thanks @danmullen – New Logo Oct 03 '14 at 21:57
  • @NewLogo: it took 21 minutes for the 'short but right' answer to be delivered to you - if you wouldn't mind searching for answers prior to asking, you'll get them even faster! – halfer Oct 03 '14 at 22:09
  • 1
    Glad you managed to fix it @New Logo – danmullen Oct 03 '14 at 22:16
  • you are right @halfer but i never thought the position of my – New Logo Oct 03 '14 at 22:46
1

Headers already sent" means that your PHP script already sent the HTTP headers, and as such it can't make modifications to them now.

Check that you don't send ANY content before calling session_start. Better yet, just make session_start the first thing you do in your PHP file (so put it at the absolute beginning, before all HTML etc).

halfer
  • 19,824
  • 17
  • 99
  • 186
ThatMSG
  • 1,456
  • 1
  • 16
  • 27
  • The quote device is useful when you are quoting other people - if you are just writing your own material, it's best without. – halfer Oct 03 '14 at 22:07