PDA

View Full Version : PHP - cookies... style switcher


Joe 90
06-10-2007, 14:24
Howdo,

yesterday i tried to make a php based style switcher... and now my site wont load my style sheets, but rather than give in - i'm still in search of an answer...

anyway, heres my code -
<link rel="stylesheet" href="<?php print $currentstyle; ?>" type="text/css" />

<?php
// Set Default Style
$currentStyle = 'bridge.css';
// Check to see if the page was posted back
if(isset($_POST["submit"]))
{
// Find out if they want to set or delete a cookie
if($_POST["style"] == "none")
{
// Unset the Cookie
setcookie("style", "", time()-288000);
}
// Set Current Style
$currentStyle = $_POST["style"] . ".css";
// Set the requested Style to a cookie
setcookie("style", $_POST["style"], time()+288000);
} else {

// Check Cookie for Style
if(isset($_COOKIE["style"]))
{
// Use Style From Cookie
$currentStyle = $_COOKIE["style"] . ".css";
} else {
// Set Default Style
$currentStyle = 'bridge.css';
}
}
?>

<form method="post" action="../css/setstylecookie.php">
<select name="style">
<option value="none">No Styles</option>
<option value="bridge">Bridge</option>
<option value="show">Old School</option>
<input type="submit" name="submit" value="set" />
</select>
</form>
then obviously i have 3 CSS files - bridge.css, show.css and none.css

and my dir tree is a little bit like...

./
./head.php
./includes
../css/
..../bridge.css
..../setstylecookie.php
../php/
..../submenu.php

don't suppose we have any php legends in here do we? ;D

Mark
06-10-2007, 20:01
Dunno about legend, but proficient, maybe. ;)

Anyway, what does the 'view source' output look like?

Edit - no matter, found it.

Looks like $currentstyle isn't set. Unfortunately, I don't have enough information to work out where it gets set since I don't know the relationship between head.php and setstylecookie.php

Joe 90
06-10-2007, 20:07
link in sig ;)

http://www.marksweb.co.uk/main.php

as you can see the style isn't loading and there is a style selection form on there near the top :)

*edit*
i've force'd the default style sheet (yes i know i've not sorted it to display both search and form on the same line) because the script isn't working, but if you use the form to select the style you'll get the same results of the site not loading any styles! :(

Mark
06-10-2007, 20:07
See edit. ;)

Joe 90
06-10-2007, 20:19
okay - every page on the site uses include statements to load the head.php file i've got.

setstylecookie.php is sitting in the css dir and i suspect it is the cause of the problem - something relating to the form not being able to get at the script possibly.

Having said that when you submit the form, the setstylecookie page does load - so maybe something within it doesn't work - furthermore if you set the cookie again it adds an extra "/includes" to the url which already has the correct path including the "/includes" folder.

leowyatt
06-10-2007, 20:59
Hey mate, my brain is battered at the moment. I've got to do some work tomorrow morning and will gladly take a look if you want.

Mark
06-10-2007, 20:59
My question stands. How does the variable get set in/before head.php? :huh:

Joe 90
06-10-2007, 21:06
ah yes...lol

did think of the way my includes were being brought in - obviously there needs to be inclustion of a script creating a variable before anything uses that variable.

i'll have a quick play around and try to put the script in the head before the head tags that way it'll hopefully fit in with ever page of my site better.

/note to self - stop making newbish errors.


*edit*
this now works...but due to the way my site gets 'built' with all the includes and the complex directory tree its a little hard to get it working now after its loaded the script it just sits on the same file containing the script which then other than loading the style sheet set by the cookie doesn't do anything than contain the head tags.

I'm guessing it'd then be fine to redirect the head file to the main page which'd then pull out the correct style... (slightly complicated i know)

omg that wont even work because if the head is included in every page and then the script and the style variable is in every head it'd never work because even a redirect would cause you to end up at the main page every time... bloody hell.

Mark
06-10-2007, 21:21
You need two script functions - one to set the cookie and one to get the cookie and fill in the variable from it (with appropriate default). Right now I think you have both in one function which might be what's causing the confusion.

PHP does allow you to write procedural functions (and class methods for that matter), so you can put both in one file and call the right one if you like.

One thing I'm confused about - Why would you want to redirect from an include file anyway? I'm not sure you're using includes properly if you're doing that.

Oh, and you really ought to secure the includes directory so that a user like me can't browse into it. :p :)

Joe 90
06-10-2007, 21:54
right, this is gettin intense.

anyway, lol, how'd you browse into it? i just assume most people will get to a 403 Forbidden error and assume its unavailable.

and how'd you secure an include statement without making the user unable to load the actual contents of the file?

Mark
06-10-2007, 22:04
I did need 'inside information' to do it, but it's something to bear in mind. You can secure the includes directory by putting a file called .htaccess in there, containing:
<Limit GET>
order deny,allow
deny from all
</Limit>
Or something like that.

Joe 90
06-10-2007, 22:36
well hopefully my dirs are secure now (still not sure how u did it)