Hi there. I hope you guys can help 🙂

This works on local host but once online it fails with "PHP Warning: session_start(): Cannot start session when headers already sent in /home/martinsb/public_html/switcher/switch.php on line 10"

Here is the nav part

<!---new code added for style switcher-->

Style is set to <?php echo $_SESSION["msg"];?><br/>


    <a class="<?php echo $_SESSION['look0'];?>" href="switcher/switch.php?set=default">Default</a>
    <a class="<?php echo $_SESSION['look1'];?>" href="switcher/switch.php?set=ada">ADA</a>
    <a class="<?php echo $_SESSION['look2'];?>" href="switcher/switch.php?set=burrito">Burrito</a><br/>
    <div style="font-size:12px; padding-top:8px; padding-bottom:10px;"><?php echo $_SESSION["why"];?></div>

<!--end switcher-->

Here is the switcher.php

<?php

$part= $_SERVER['HTTP_REFERER'];
$path_parts = pathinfo($part);
 $result = $path_parts['basename'];

session_start();

?>
<?php
if( !isset($_GET['set'])){
echo "";
}


elseif ($_GET['set']=='default') {


    $_SESSION["css"]="css/mysite.css";
    $_SESSION["msg"]="Default";
    
    
    }    

elseif ($_GET['set']=='dark') {


    $_SESSION["css"]="css/darkstyles.css";
    $_SESSION["msg"]="Dark";
    
    
    }

elseif($_GET['set']=='light') {


    $_SESSION["css"]="css/lightstyles.css";
    $_SESSION["msg"]="Light";

    
    }

if ( !isset($SESSION["css"]) )
{$
SESSION["css"]="css/mysite.css";
$_SESSION["msg"]="Default";


    }


?>
<?php
// 301 Moved Permanently

header("Location: ../$result", true, 301);

exit();
?>

MartinBurrito it fails with "PHP Warning: session_start(): Cannot start session when headers already sent in /home/martinsb/public_html/switcher/switch.php on line 10"

Could you start by showing us the first dozen lines or so of the file mentioned in the above error message -- preferably within this forum's [code=php]...[/code] tags to keep it readable?

MartinBurrito This works on local host but once online it fails...

In the case of a "headers already sent" error, I've seen this in the past when uploading files via SFTP to the web server, and the FTP client thinks it needs to add a BOM (Byte Order Mark) at the very start of the file because it's UTF-8 or such. If you are FTP-ing the files, might want to check your FTP client for any config options that will disable that "feature". (If there is anything, including white-space or non-printing characters before the opening <?php tag, it will cause HTTP headers to be sent immediately.)

    5 days later

    MartinBurrito

    MartinBurrito "PHP Warning: session_start(): Cannot start session when headers already sent in /home/martinsb/public_html/switcher/switch.php on line 10"

    As soon as any PHP script outputs any text at all, headers get sent and you cannot call session_start(). This can happen quite easily if you have any text before your first <?php tag or after some included file's closing ?> or because of an echo or var_dump statement, etc.

    It's hard to tell from your post because your code is formatted strangely, but it looks like you have an HTML comment before your opening PHP tag:

    <!---new code added for style switcher-->
    
    Style is set to <?php echo $_SESSION["msg"];?><br/>

    If that code runs before some other php code that calls session_start then you'll get a complaint because you are outputting text to the browser already.

    If it works on one machine but not another, there may be some difference in configuration settings. For example, you may have a different error_reporting setting that's more relaxed on localhost and more finicky on your server.

      3 months later
      Write a Reply...