0

I have newly registered my domain and everything is working fine but when i use session_start() on my php pages i get this warning

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at D:\inetpub\vhosts\mywebsite.com\httpdocs\sample\new.php:1) in D:\inetpub\vhosts\mywebsite.com\httpdocs\sample\new.php on line 3

 `Warning: session_start() [function.session-start]: Cannot send session 
  cache limiter - 
  headers already sent (output started at        
  D:\inetpub\vhosts\mywebsite.com\httpdocs\sample\new.php:1) in 
  D:\inetpub\vhosts\mywebsite.com\httpdocs\sample\new.php on line 3`

What do i need to do to get rid of this warning I an unable to use session_start() on my php pages.please help me in fixing this.any help is greatly appreciated.Thanks

Update This is my index and profile.php pages

my index.php page

     <?php
          session_start();
       ?>
               <!doctype HTML> some html coding goes on

my profile.php page

 <?php
            session_start();
    $con = mysql_connect('some', 'some', 'some');       
    $email = $_POST["nemail"];
    $password = $_POST["npassword"];
    $month = $_POST["nmonth"];
    $dayy = $_POST["ndate"];
    $yearr = $_POST["nyear"];
    $gender = $_POST["ngender"];
    $sname = $_POST["sname"];
    $status = $_POST["nrelation"];
    $rannum = 1;
    $birthday = $month . " " . $dayy . " " . $yearr;
    mysql_select_db("users_names");
            $_SESSION['unique']= $rannum;
  ?>

I get this error when i jump to profile.php from index.php

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at D:\inetpub\vhosts\website.com\httpdocs\sample\profile.php:1) in D:\inetpub\vhosts\website.com\httpdocs\sample\profile.php on line 3

I want to start session on index page but i want to assign values to session variables in profile.php page thats what i did in profile.php in the last line

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
niko
  • 9,285
  • 27
  • 84
  • 131
  • Nothing is there in my code i just used session_start() in my page thats it i see this error nothing else in my code and when i remove the session_start() i dont get this warning – niko Jan 25 '12 at 08:34
  • 1
    So it's nothing with your code but when you modify it the error don't appear... strange... maybe is there something with your code... can you post it ? – BlackCharly Jan 25 '12 at 08:52

4 Answers4

4

Are your php scripts in UTF (I mean content of php files is in unicode encoding)? Did you remove BOM record in that case? Or, perhaps, you have empty space(s) before opening php tag

Wiki

For instance in PHP, the existence of a BOM will cause the page to begin output before the initial code is interpreted, causing problems if the page is trying to send custom HTTP headers (which must be set before output begins).

Cheery
  • 16,063
  • 42
  • 57
  • Yes they are UTF 8 and what is BOM – niko Jan 25 '12 at 08:53
  • @niko I also gave a link to Wiki http://en.wikipedia.org/wiki/Byte_order_mark it is set of bytes at the beginning of the file. It will be the first output to the browser. Cite from Wiki: For instance in PHP, the existence of a BOM will cause the page to begin output before the initial code is interpreted, causing problems if the page is trying to send custom HTTP headers (which must be set before output begins). – Cheery Jan 25 '12 at 08:56
  • okay may be i dont know but how do i remove bom can you explain in your answer please – niko Jan 25 '12 at 09:12
  • @niko You have either to download everything to your computer and to remove BOM manually in the any normal text editor or to use any utility on your computer or one, that you can run on your server, to fix the files. And look here http://stackoverflow.com/questions/3255993/how-do-i-remove-i-from-the-beginning-of-a-file – Cheery Jan 25 '12 at 09:14
  • 1
    @niko try it on just one file and check the result. Notepad++ easily removes BOM (Encoding in menu). Remove it, upload back to the server, call it through the browser and look at result. If warning disappears - you've found the reason. – Cheery Jan 25 '12 at 09:21
  • Thanks cheery but all i can do is i can give you +1 but it was his answer open my eyes – niko Jan 25 '12 at 18:34
  • @niko it is up to you. I wrote "Or, perhaps, you have empty space(s) before opening php tag" at my first posting. I do not care about points - I just like to help ) – Cheery Jan 25 '12 at 18:39
  • well i appreciate your help and im really thankful to u thanks cheery – niko Jan 25 '12 at 19:18
1

With your code posted it will be simplier.

The cookies (session_start use cookies) can only be sent with the HTTP headers. If you've already done a echo or called the function headers() before the session_start() function, you can't send cookies anymore so you can't start the session, because the headers are already sent.

BlackCharly
  • 649
  • 4
  • 8
  • I guess you are right i used headers in my page im not sure where i used let me check it and Thanks – niko Jan 25 '12 at 08:44
  • @niko I told you - check for spaces, new line symbols before the – Cheery Jan 25 '12 at 08:54
  • @niko You definitely have BOM - remove it from all files. To check whether you have it or not - download file by ftp and open in any normal text editor (like Notepad++ or whatever you use for scripts editing) – Cheery Jan 25 '12 at 09:11
  • The session_start() is the first function you call, check there is **nothing** before the ` – BlackCharly Jan 25 '12 at 09:12
  • @BlackCharly Your awesome there was spaces before – niko Jan 25 '12 at 09:25
  • @niko I wrote you about spaces too and thought that you've checked it already. – Cheery Jan 25 '12 at 09:27
1

Make sure absolutely nothing - not even whitespace - is output before the call to session_start().

marramgrass
  • 1,411
  • 11
  • 17
0

You need to enable output beffering to be able to use session. Use ob_start() function somewhere in the beginning of you php code

heximal
  • 10,327
  • 5
  • 46
  • 69
  • @nikc.org : With ob_start() enabled at script begining, the headers aren't directly sent and the session can be started at every point of the script, so it has to do with the error. – BlackCharly Jan 25 '12 at 08:49
  • 1
    @BlackCharly unless the file has a BOM at the beginning. Then all the output buffering in the world will have no effect whatsoever. Using output buffering can be _a solution_, but it is not an explanation to what the source of the problem is. – nikc.org Jan 25 '12 at 08:56