Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   Changing Style Sheets with PHP (http://www.programmingforums.org/showthread.php?t=11943)

Xeoncross Nov 19th, 2006 2:37 PM

Changing Style Sheets with PHP
 
Ok, so not everyone has the same monitor and not everyone has 20-20 vision. So, since the "Ctrl++" trick is not well known you have to make a font size chooser for your site. So here is what I have done:

stylesheets.php:

[PHP]<?php
$ref = (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : "http://{$_SERVER['SERVER_NAME']}/";

if ((isset($_GET['stylesheet'])) && (strlen($_GET['stylesheet']) < 20)) {
$stylesheet = $_GET['stylesheet'];
$stylesheet = strip_tags($stylesheet);
if (!setcookie("stylesheet", $stylesheet, time()+(7 * 24 * 60 * 60)) ) die ("could not set cookie!");

header("Location: $ref");

} else {
header("Location: $ref");
}

?>[/PHP]

and then I have this at the top of my pages:


[PHP]<?php

if (!isset($_COOKIE["stylesheet"])) {
echo "there is no cookie";
$stylesheet = 'white';
} else if ((isset($_COOKIE["stylesheet"])) && ( (strlen($_COOKIE["stylesheet"]) < 25) && (strlen($_COOKIE["stylesheet"]) > 1) ) ) {
echo "there is a cookie!";
$stylesheet = strip_tags($_COOKIE["stylesheet"]);
echo $stylesheet;
} else {
echo "the cookie is too big!";
$stylesheet = 'white';
}
$form = ' <a href="/stylesheets.php?stylesheet=white">White</a> - '
.'<a href="/stylesheets.php?stylesheet=blue">Blue</a> '
.'<a href="/stylesheets.php?stylesheet=green">Green</a> ';
?>[/PHP]


And then this in my header:

[PHP]<link rel="stylesheet" type="text/css" href="/themes/code2design/<?php echo $stylesheet; ?>.css" media="screen,projection" />[/PHP]

Now for some reason I get random style sheets, like i could click on "stylesheet=white" and after the cookie is set and I am sent back to the page (see stylesheets.php) my code shows that "blue" is the value for "stylesheets"??? And just other weird things...


Here are some similar scripts:

http://www.dyn-web.com/dhtml/sizefont/sizerdx.php
http://raena.net/2003/12/06/return-of-the-...-size-together/
http://alistapart.com/articles/phpswitch/
http://www.joe2torials.com/view_tutorial.php?view=52
http://www.456bereastreet.com/archive/2006...sheet_switcher/
http://www.gr0w.com/articles/code/css_styl...g_php/index.php

Eryk Nov 19th, 2006 2:51 PM

Could you supply a link? Or is this on a personal server?

Xeoncross Nov 19th, 2006 2:52 PM

Sorry, I can't supply a link... but it is running on a 1and1.com server.

:::::::::::::::::::::::::::::::::

Ok, weird.

For the first time ever, IE is actually working with this code while the problem seems to be with FireFox?!? In IE the style changes just fine... but in firefox it chooses its own style.

Eryk Nov 19th, 2006 2:57 PM

Quote:

Originally Posted by Xeoncross (Post 119332)
Sorry, I can't supply a link... but it is running on a 1and1.com server.

:::::::::::::::::::::::::::::::::

Ok, weird.

For the first time ever, IE is actually working with this code while the problem seems to be with FireFox?!? In IE the style changes just fine... but in firefox it chooses its own style.

Well that would mean that its not a server-side error, but rather the way its handled by the client. Perhaps something wrong with setting the cookies. After the cookie supposed to have been sent go to
Tools > Options > Privacy > Show Cookies
Then search for your site. See what the cookie says. Or in the URL bar you could just type:
:

javascript:alert(document.cookie);void(0);

Xeoncross Nov 19th, 2006 3:24 PM

FireFox creates two cookies
 
Ok...

Seems FireFox creates two cookies named

:

stylesheet=blue
stylesheet=green


How can FF get away with two different cookies named the same? Also, I looked at the cookies with the Web Developer 1.0.2 addon for firefox 2 and it only shows one cookie; blue.

Eryk Nov 19th, 2006 4:47 PM

Does one have www. before and the other not?

piercy Nov 22nd, 2006 2:24 PM

so.... it would seem the page doesnt know which cookie to handle.

:

$cookie = $_COOKIE['stylesheet'];

if(!empty($cookie)) {
setcookie("stylesheet", "", time()-3600); //set cookie to expire a hour before(basically remove cookie)
}
setcookie("stylesheet","COOKIE INFO HERE",time()+3600);


Think that should work (although i havent tested). I know that removing cookie then recreating shouldnt do much but if FF is duplicating at least this stop it.


All times are GMT -5. The time now is 1:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC