0

I'm getting two warnings:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent on line 2 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent on line 2

This is my index.php (this file is called first):

include ('funkcije.php');
session_start();
if(!isset($_SESSION)) {session_start();}
if(isset($_POST['login']))
            { 
    Log($skripta);
            }

This is my code (funkcije.php //this file is included and here are functions definitions):

<?php
if(!isset($_SESSION)) {session_start();} // On this line is problem

$skripta=$_SERVER['SCRIPT_NAME'];
$skripta=basename($skripta);

function Menu() {
?>
<html>
<ul>
<li> <a href="index.php?akc=logout"> Logout </a> </li>
</ul>
</body>
</html>
<?php
}

Can someone please help me to resolve this problem?

user2205591
  • 23
  • 1
  • 7
  • do you got any output before the `session_start()`? – Class Apr 03 '13 at 23:20
  • Is this your entire file? Or is there a space, or anything else (HTML, ANYTHING), above the ` – Luke Shaheen Apr 03 '13 at 23:20
  • @John @Class I have one "index" file which starts session: ` – user2205591 Apr 03 '13 at 23:31
  • 1
    @user2205591 Something in funkcije.php is outputting information to the browser. Debug by removing that include; if that fixes your problem, then try removing chunks of funkcije.php until you find the problem code line. – Luke Shaheen Apr 03 '13 at 23:34
  • @John It's strange because my code works normally when I'm trying it locally on my xampp, but it doesn't work uploaded on real server – user2205591 Apr 03 '13 at 23:48
  • @user2205591 Have you tried what I suggested to debug? First step would be to remove anything above `session_start()` - that includes `include ('funkcije.php');` – Luke Shaheen Apr 03 '13 at 23:50
  • @John I edited question and putted begin of both files. Somewhere here is problem – user2205591 Apr 04 '13 at 00:05
  • @user2205591 Try checking for the session in a "more correct" way - `if(session_id() == '')` – Luke Shaheen Apr 04 '13 at 00:07
  • @John regardless I remove `if` statement and just put session start, there is still warning – user2205591 Apr 04 '13 at 00:29
  • @user2205591 Either way, I suggest replacing your `isset` statement with what I said. See here: http://stackoverflow.com/questions/6249707/check-if-php-session-has-already-started This might not fix your issue, but it does make your code better. – Luke Shaheen Apr 04 '13 at 00:36
  • @John Ok. I replaced `isset`. Do you think maybe is problem in my server or server's php.ini file? – user2205591 Apr 04 '13 at 00:47
  • @user2205591 Honestly, I'm not sure, sorry! I'd suggest you continue to work with Dominik on his answer, he might have better insight. – Luke Shaheen Apr 04 '13 at 01:03

1 Answers1

0

This error means you have outputted something before calling the session_start function. Make sure there is nothing above your <?php tag and no outputs before session_start.

edit as question evolved:

try this: index.php

if(session_id() == '') session_start();
include ('funkcije.php');
if(isset($_POST['login']))
{ 
    Log($skripta);
}

funkcije.php

<?php    
$skripta=$_SERVER['SCRIPT_NAME'];
$skripta=basename($skripta);

function Menu() {
    echo '<html>
    <ul>
    <li> <a href="index.php?akc=logout"> Logout </a> </li>
    </ul>
    </body>
    </html>';
}
Dominik
  • 6,078
  • 8
  • 37
  • 61
  • It's strange because my code works normally when I'm trying it locally on my xampp, but it doesn't work uploaded on real server – user2205591 Apr 03 '13 at 23:33
  • perhaps your host is injecting code for statistical reasons? Is there more in this file? Perhaps your calling an include twice and the second time causes this error as by that time you have outputted something... – Dominik Apr 03 '13 at 23:35
  • How can I check if problem is with my server or php.ini on server? I don't know if my host is injecting statistical code – user2205591 Apr 04 '13 at 01:10
  • that would be unlikely. test the other things first. how do you call this file? is it included somewhere? is there code above the ode you posted? – Dominik Apr 04 '13 at 01:14
  • ok seeing you code now I suggest you move the `if(!isset($_SESSION)) {session_start();}` to the top above your include and remove it everywhere else. is the problem still there and if so whats the warning? – Dominik Apr 04 '13 at 01:17
  • I managed to find problem (it was in BOM) and then I had problem with `header(Location:)` that I solved with output buffering. If you have any advice I will be glad to hear – user2205591 Apr 04 '13 at 13:05