![]() |
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 |
Could you supply a link? Or is this on a personal server?
|
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. |
Quote:
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); |
FireFox creates two cookies
Ok...
Seems FireFox creates two cookies named :
stylesheet=blueHow 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. |
Does one have www. before and the other not?
|
so.... it would seem the page doesnt know which cookie to handle.
:
$cookie = $_COOKIE['stylesheet'];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