Jump to content

style switcher


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 post
Share on other sites

If cookies are blocked they probably won't be able to use the session either. I guess the querystring would be the most foolproof in terms of the browser not needing anything to make it work, but I would just use a session.

Link to post
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 post
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 post
Share on other sites

Every page that uses session variables needs, at the very top of the page

session_start();

Link to post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...