Boat Drinks  

Go Back   Boat Drinks > General > Computer and Consoles

Reply
 
Thread Tools Display Modes
Old 06-10-2007, 14:24   #1
Joe 90
Absinthe
 
Joe 90's Avatar
 
Join Date: Jan 2007
Location: Chester
Posts: 2,345
Default PHP - cookies... style switcher

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 -
Quote:
Originally Posted by head.php
Code:
<link rel="stylesheet" href="<?php print $currentstyle; ?>" type="text/css" />
Quote:
Originally Posted by setstylecookie.php
Code:
<?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';
			}
	}
?>
Quote:
Originally Posted by submenu.php
Code:
	<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...
Code:
./
./head.php
./includes
../css/
..../bridge.css
..../setstylecookie.php
../php/
..../submenu.php
don't suppose we have any php legends in here do we?
__________________
360 Blog | Join GiffGaff | Twitter

Last edited by Joe 90; 06-10-2007 at 15:30.
Joe 90 is offline   Reply With Quote
Old 06-10-2007, 20:01   #2
Mark
Screaming Orgasm
 
Join Date: Jul 2006
Location: Newbury
Posts: 15,194
Default

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

Last edited by Mark; 06-10-2007 at 20:06.
Mark is offline   Reply With Quote
Old 06-10-2007, 20:07   #3
Joe 90
Absinthe
 
Joe 90's Avatar
 
Join Date: Jan 2007
Location: Chester
Posts: 2,345
Default

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!
__________________
360 Blog | Join GiffGaff | Twitter

Last edited by Joe 90; 06-10-2007 at 20:22.
Joe 90 is offline   Reply With Quote
Old 06-10-2007, 20:07   #4
Mark
Screaming Orgasm
 
Join Date: Jul 2006
Location: Newbury
Posts: 15,194
Default

See edit.
Mark is offline   Reply With Quote
Old 06-10-2007, 20:19   #5
Joe 90
Absinthe
 
Joe 90's Avatar
 
Join Date: Jan 2007
Location: Chester
Posts: 2,345
Default

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.
__________________
360 Blog | Join GiffGaff | Twitter

Last edited by Joe 90; 06-10-2007 at 20:21.
Joe 90 is offline   Reply With Quote
Old 06-10-2007, 20:59   #6
leowyatt
Chef extraordinaire
 
leowyatt's Avatar
 
Join Date: Jul 2006
Location: Infinite Loop
Posts: 11,143
Default

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.
__________________
"Dr Sheldon Cooper FTW!"
leowyatt is offline   Reply With Quote
Old 06-10-2007, 20:59   #7
Mark
Screaming Orgasm
 
Join Date: Jul 2006
Location: Newbury
Posts: 15,194
Default

My question stands. How does the variable get set in/before head.php?
Mark is offline   Reply With Quote
Old 06-10-2007, 21:06   #8
Joe 90
Absinthe
 
Joe 90's Avatar
 
Join Date: Jan 2007
Location: Chester
Posts: 2,345
Default

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.
__________________
360 Blog | Join GiffGaff | Twitter

Last edited by Joe 90; 06-10-2007 at 21:14.
Joe 90 is offline   Reply With Quote
Old 06-10-2007, 21:21   #9
Mark
Screaming Orgasm
 
Join Date: Jul 2006
Location: Newbury
Posts: 15,194
Default

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.

Last edited by Mark; 06-10-2007 at 21:24.
Mark is offline   Reply With Quote
Old 06-10-2007, 21:54   #10
Joe 90
Absinthe
 
Joe 90's Avatar
 
Join Date: Jan 2007
Location: Chester
Posts: 2,345
Default

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?
__________________
360 Blog | Join GiffGaff | Twitter
Joe 90 is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 06:17.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.