CSS Switcher works local not online

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MartinBurrito
Forum Newbie
Posts: 1
Joined: Fri Mar 19, 2021 7:22 pm

CSS Switcher works local not online

Post by MartinBurrito »

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();
?>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: CSS Switcher works local not online

Post by Benjamin »

The error message is giving you the location of where the output is being sent. session_start(); must be executed before that specific location.

Also, session_start(); should be called before you start using the $_SESSION variables too.
Post Reply