Jump to content

style switcher


vchris
 Share

Recommended Posts

What's the best way to create a PHP style switcher?I've looked a bit into it but I want something as clean as possible and that'll always work. I don't really wanna use the querystring. Cookies are good but what about if cookies are blocked? Session variable?

Link to comment
Share on other sites

The session is more reliable then cookies are, if all you need is non-persistent data. The best way to check the session might just be to set a value and check it on another page. Otherwise you would need to check a couple settings in php.ini and then try to figure out if the user has cookies enabled.

Link to comment
Share on other sites

So I got the style switcher kinda working. Only problem I have is the Session variable doesn't seem to be there even though I set it.Styleswitcher.php

<?php//Set Session variable style$_SESSION['style'] = $_REQUEST['style'];//Redirect user to page he was onheader('Location: '.$_SERVER['HTTP_REFERER']);?>

Testpage.php

<p><a href="/scripts/styleswitcher.php?style=red">R</a> <a href="/scripts/styleswitcher.php?style=green">G</a> <a href="/scripts/styleswitcher.php?style=blue">B</a></p><?php//$_SESSION['style'] = red;echo '<p>style='.$_SESSION['style'].'</p>';?>

When attempting to display the style on the test page I get nothing. Simply "style=". But if I uncomment this line //$_SESSION['style'] = red; I get "style=red". I also tested displaying the session variable style value in the styleswitcher.php file and it worked fine. Could it have something to do with the header function?Why doesn't it work when I set the session variable style in the styleswitcher.php file?

Link to comment
Share on other sites

You'll also want to use session_write_close before you send the header. But, some browsers have problems with this because if you send some browsers a location header, they will ignore other headers (like the cookie header) and never save the session cookie. You can work around that by just using a meta refresh instead to force the browser to save the cookie first, then redirect.

<?phpsession_start();//Set Session variable style$_SESSION['style'] = $_REQUEST['style'];session_write_close();echo "<html><head><title>Thank You</title><meta http-equiv=\"refresh\" content=\"1;url={$_SERVER['HTTP_REFERER']}"></head><body>Thank you</body></html>";?>

Link to comment
Share on other sites

The session will automatically close. You would only need to use something like session_write_close if you're doing a header redirect, but that doesn't work for every browser anyway so that's why you see most sites online using a meta refresh page after you log in or out.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...