Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Brendon Bauer

macrumors 6502
Original poster
May 14, 2007
344
0
Good 'ol USofA
I've set up a CSS switcher using the instructions at A List Apart (http://www.alistapart.com/articles/phpswitch/). Everything works great except for the fact that it only works for files that are contained with my 'html' directory. If you go to my website and change the theme, it works for all the links at the top except the ones that take you to a different directory of the website. I cannot figure this out. I had originally set up a javascript css switcher which worked fine but that's not as good because not everyone has javascript enabled. This php one would be great except for the fact that it doesn't work on the wordpress installations, the phpbb forum and the mailing list subscribe/unsubscribe page. When you go to one of those pages after changing the default theme (green) to something else it reverts back to the default as if it couldn't find the cookie...

Can anyone figure this out? :\
 
I know what my problem is, but I don't know how to fix it. In the switcher.php file I have
Code:
<?php
setcookie ('sitestyle', $set, time()+1209600, '/', 'summitnorthwest.org', '0');
header("Location: $HTTP_REFERER");
?>

My problem is that it is set to '/' and the page I'm setting the cookie at is located at /html/index.php. This means that when the cookie is set, it only applies to /html/. How can I tell it to apply it to the whole domain? I tried using '../' and './' (I'm not sure which one means up directory but one of them does... can't remember though) but neither of those worked. In fact when I did that it just broke it. I also tried using 'summitnorthwest.org/' and 'summitnorthwest.org' but those broke it as well. Any ideas?
 
Ok so after I view my cookie in safari, it looks like it's applied to the whole domain. Why, then, does it not work in the wordpress and forum parts of the website? If you look at the source code it reverts back to the default (green.css) as if it didn't find the cookie?
 
I switched to the blue theme and it worked on some pages, but not on the Video Sermons section. Though looking at the cookie for that page it said the theme was set for blue, which means reading the cookie for that page is awry. We may need to see more of your code. I use to have a CSS switcher on my site using JavaScript to change the styles. My code was a little different from ALA's though, but didn start with their code.
 
Ok, I'll let you in on all of the code.

First, this is my switcher.php file located at /html/includes/switcher.php:
Code:
<?php
setcookie ('sitestyle', $set, time()+1209600, '/', 'summitnorthwest.org', '0');
header("Location: $HTTP_REFERER");
?>
I should let you know that the <?php setcookie segment is not exactly like the one from A List Apart. I took the advice of another website to update that to the code above. I've already tried it with the old one and it didn't make a difference.

Next is the file called styles.php that is included in the <head> tag of every page of my website (including the pages that aren't working). It's location is /html/includes/styles.php.
Code:
<link href="http://www.summitnorthwest.org/html/includes/theme/persistent.css" rel="stylesheet" type="text/css" media="screen" />

<link rel="stylesheet" type="text/css" media="screen" title="User Defined Style" href="http://www.summitnorthwest.org/html/includes/theme/<?php echo (!$_COOKIE['sitestyle'])?'green':$_COOKIE['sitestyle'] ?>.css" />

<link href="http://www.summitnorthwest.org/html/includes/theme/green.css" rel="alternate stylesheet" type="text/css" title="green" media="screen" />

<link href="http://www.summitnorthwest.org/html/includes/theme/red.css" rel="alternate stylesheet" type="text/css" title="red" media="screen" />

<link href="http://www.summitnorthwest.org/html/includes/theme/blue.css" rel="alternate stylesheet" type="text/css" title="blue" media="screen" />

That last bit is the links themselves to change the style in index.php.
Code:
<p><i><font color="red">Beta</font></i> Change Theme: <a class="nounderline" href="includes/switcher.php?set=green"><font color="green">Green</font></a>, <a class="nounderline" href="includes/switcher.php?set=red"><font color="red">Red</font></a>, <a class="nounderline" href="includes/switcher.php?set=blue"><font color="blue">Blue</font></a></p>

At the top of index.php in the <head> tag I have the php include for the styles.php file. It is included using the relative location from index.php but on every file outside of the /html/ directory the include has the full url rather than "includes/styles.php".

Hope this helps :) Just a side note: I started by using the javascript code from A List Apart and that worked on every page of the website. I would rather use a PHP switcher, but if we can't figure this out I may end up having to use javascript.

Thanks for coming to the rescue!
 
One more small note: The Audio Sermons, Video Sermons, and Notes from the Pastor pages are all run by separate wordpress installations. The Discussion Board is a phpbb forum. The Mailing List pages (past the first page you see) is phplist. It is all of these that don't work, and I'm guessing it's because they are not contained in /html/ but to be honest I have no idea.
 
I haven't messed with cookies from PHP but you can try some things. I found two other ways to access the cookie data that can try out to see if you get different results.

PHP:
$HTTP_COOKIE_VARS["sitestyle"];
or
$_REQUEST["sitestyle"];

A small thing I noticed different in your code from others I've been looking through, on your setcookie line others would have added a dot before summitnorhtwest.org. Have no idea if that's a big deal or not, but something else to try.
 
Tried the two changes you suggested as well as adding the period before the domain. Nothing changed. It all still worked to set and retrieve cookies but for some reason wordpress, phpbb and phplist will not pull the cookie. This is bizarre.

I might just go back to javascript :\

Edit: Danget, I knew there would be a tradeoff. I went back to Javascript and everything works except one page. When you go to the mailing list page and then continue to the phplist directory by clicking on subscribe or unsubscribe, it doesn't work :(. Drat. This makes me angry!
 
I see one thing different on that problem page from the others,

HTML:
<meta http-equiv="Cache-Control" content="no-cache, must-revalidate" />
<meta http-equiv="pragma" content="no-cache" />

It's possible this is interfering with cookie control. Also on that page there's a JavaScript error:
Error: a.getAttribute("rel") has no properties
Source File: http://www.summitnorthwest.org/html/includes/styleswitcher.js
Line: 4


The other pages don't get this error, but not sure why. It may be related to the no caching statements above.

My only other idea on the PHP problem is that maybe those other installs (WordPress, etc.) having something going on that is interfering.
 
I tried removing those two lines and it didn't work :(. I'm sure it's something so small I'll never even find it :eek:

I may have found that small thing. The styleswitcher code looks for link tags with 'style' in the rel attribute and have a title attribute. The line below doesn't have a rel attribute (and doesn't need one) so causes the script to error out before finishing. Things should work if you remove this line. I'm pretty certain on this.
HTML:
<link rev="made" href="mailto:phplist%40tincan.co.uk" />
You can either remove that line or change the JavaScript,
Code:
line 4:
if(a.getAttribute("rel") && a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title"))

Inside getActiveStyleSheet function:
if(a.getAttribute("rel") && a.getAttribute("rel").indexOf("style") != -1
    && a.getAttribute("title") && !a.disabled)

Inside getPreferredStyleSheet function:
if(a.getAttribute("rel") && a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       )
 
Wow, good job! You were right. Removing it worked, but I also tried just moving it below the tag where I called in the style sheet and that worked as well. Figured it was there for something so I might as well keep it. Thanks for finding that! I guess I'll stick with javascript since it seems to work more places. Most of our viewers have javascript enabled anyways... I put a <noscript> message just in case, though. Thanks again! I'm sure I'll be back with more questions about something else in the future :).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.