/    Sign up×
Community /Pin to ProfileBookmark

ALA PHP Styleswitcher problems…

Hello. On my website, I am currently using a JS styleswitcher to allow users to switch between the old style and the new one. However, I received a comment that it isn’t working, even when they have JS enabled. Because of this, I decided to implement [url=”http://alistapart.com/articles/phpswitch/”]the PHP styleswitcher they have at ALA[/url].

I made a file and titled it switcher.php, and it had the following source in it:

[code=php]<?php
setcookie (‘sitestyle’, $set, time()+31536000, ‘/’, ‘216.36.173.149’, ‘0’);
header(“Location: $HTTP_REFERER”);
?>[/code]

I then made a link to it with this html:

[code]<a href=”switcher.php?set=white”>White</a>[/code]

Then, I modified my <link> tag that would normally import the stylesheet, to make it look like this:

[code]<link rel=”stylesheet” type=”text/css” media=”screen” title=”User Defined Style” href=”<?php echo (!$sitestyle)?’orange.css’:$sitestyle ?>.css” />[/code]

When I click on the link, though, it goes to switcher.php and gives me the following PHP error messages:

[quote]

Warning: Cannot modify header information – headers already sent by (output started at C:DanielWebsitesswitcher.php:9) in C:DanielWebsitesswitcher.php on line 10

Warning: Cannot modify header information – headers already sent by (output started at C:DanielWebsitesswitcher.php:9) in C:DanielWebsitesswitcher.php on line 11

[/quote]

Anyone know what I did wrong? I really need help on this one! Thanx,
-Dan

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@JonaApr 27.2004 — [font=arial]The JavaScript one worked fine for me, but I agree you should always do it server-side to ensure that people can get the most of it. Anyway, I couldn't test it anywhere, but I can tell you what the error means: you have output some data to the browser (using echo, print, or just plain HTML) before using the cookie() and header() function. When you use cookie(), session_start(), and/or header(), you must use them before you output any data to the browser.[/font]
Copy linkTweet thisAlerts:
@Daniel_TauthorApr 27.2004 — The only PHP I've used before that is 2 includes. Would that be what's causing the problem?

-Dan
Copy linkTweet thisAlerts:
@JonaApr 27.2004 — [font=arial]If either of the includes files output anything, and I'm sure they do, yes. Include the files afterwards.[/font]
Copy linkTweet thisAlerts:
@Daniel_TauthorApr 27.2004 — OK, Thanks man!

-Dan
Copy linkTweet thisAlerts:
@JonaApr 27.2004 — [font=arial]No problem. ? [/font]
Copy linkTweet thisAlerts:
@Daniel_TauthorApr 28.2004 — OK, it's goin' crazy. When I changed the <link> tag to what it said to, it added in "orange" to make the HREF "orange.css", but for some reason, that makes it not, import the orange stylesheet, but instead use "Basic Theme"(no stylesheet). However, you CAN select the orange one by clicking on the crayon box in FF, but why won't it load with the orange stylesheet? Secondly, when it goes to the switcher.php page, it just goes there and sits there. It doesn't redirect you back to the page you were on. Help? Thanx,

-Dan
Copy linkTweet thisAlerts:
@JonaApr 28.2004 — [font=arial]And where can I see this problem in action?[/font] :rolleyes:
Copy linkTweet thisAlerts:
@Daniel_TauthorApr 28.2004 — OK, go to my site(http://216.36.173.149/) right now. I don't wanna leave it inaccessible for too long, so reply asap, please?

-Dan
Copy linkTweet thisAlerts:
@JonaApr 28.2004 — [font=arial]OK, fix it back like it was, um... I'll have to help you later. Sorry. ?[/font]
Copy linkTweet thisAlerts:
@ConorApr 28.2004 — i wrote up my own switcher if your interested heres how i did
<i>
</i>&lt;div class="menu"&gt;
&lt;div class="top"&gt;Change Style&lt;/div&gt;
&lt;a class="menu" href="?stylesheet=style" title="Default"&gt;Default&lt;/a&gt;&lt;br&gt;
&lt;a class="menu" href="?stylesheet=styleblue" title="Blue"&gt;Blue&lt;/a&gt;&lt;br&gt;
&lt;a class="menu" href="?stylesheet=stylered" title="Red"&gt;Red&lt;/a&gt;&lt;br&gt;
&lt;a class="menu" href="?stylesheet=styleorange" title="Orange"&gt;Orange&lt;/a&gt;
&lt;/div&gt;

[code=php]
<?php

if (isset($HTTP_GET_VARS['stylesheet'])) {
setcookie("mystylesheet", $HTTP_GET_VARS['stylesheet'], time()+216000);
$style = $HTTP_GET_VARS['stylesheet'];
}
elseif(isset($_COOKIE['mystylesheet'])) {
$style = $_COOKIE['mystylesheet'];

}
else
{
$style = "style";

}
?>
[/code]


and lastly to put the chosen style

[code=php]
<?php
echo '<link rel="stylesheet" href="'.$style.'.css" type="text/css">';
?>
[/code]
Copy linkTweet thisAlerts:
@Daniel_TauthorApr 28.2004 — Actually, I seem to have just discovered my problem with the ALA version. The PHP that I use to write the code for the stylesheet is located within an include. Aparently, when I include content, it doesn't parse the PHP that is in the inlcuded file until after the page has loaded.... Is there anyway to have PHP in an include and still have it parsed before the page loads? Thanx,

-Dan
Copy linkTweet thisAlerts:
@JonaApr 29.2004 — [font=arial]The way I do styleswitching is by serving a [url=http://cmm.rgabbard.com/blogs/jona/style.php]PHP file as CSS[/url]. In this way, I can use different code for different browsers (instead of using CSS hacks, although I tend to use them as well, especially for Opera since you can cloak the browser type), and have my switcher set up. Instead of running off of cookies alone, I almost always use sessions in addition.[/font]

[code=php]
<?php
session_start();

if(isset($_GET["style"])){
$_SESSION["style"] = $_GET["style"];
} else if(isset($_COOKIE["style"])){
$_SESSION["style"] = $_COOKIE["style"];
} else if(isset($_SESSION["style"])){
$_SESSION["style"] = $_SESSION["style"];
} else {
$_SESSION["style"] = "default";
}

setcookie("style",$_SESSION["style"]);
header("Content-Type: text/css");

switch($_SESSION["style"]){
case "orange":
?>
body {
background: orange;
color: white;
}
<?
break;
case "red":
?>
body {
background: red;
color: lightblue;
}
<?
break;
case "default":
?>
body {
background: blue;
color: white;
}
<?
break;
default: # if never visited
?>
body {
background: blue;
color: white;
}
<?
}
?>
[/code]


[font=arial]Of course, that's entirely a matter of preference, I would think. For me, it makes everything easier, though. Why not process everything in one file?[/font]
×

Success!

Help @Daniel_T spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.10,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...