vchris Posted September 4, 2007 Share Posted September 4, 2007 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 More sharing options...
justsomeguy Posted September 4, 2007 Share Posted September 4, 2007 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 comment Share on other sites More sharing options...
vchris Posted September 4, 2007 Author Share Posted September 4, 2007 Is there a way I can check if the session is not blocked? So you would recommend only using session, no cookies? Link to comment Share on other sites More sharing options...
justsomeguy Posted September 4, 2007 Share Posted September 4, 2007 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 More sharing options...
vchris Posted September 4, 2007 Author Share Posted September 4, 2007 But where would the link link to? a page named styleswitcher.php and take a querystring then redirect to the page the user was browsing? Link to comment Share on other sites More sharing options...
justsomeguy Posted September 4, 2007 Share Posted September 4, 2007 That would work, you could use the referer to keep track of where to redirect them. Link to comment Share on other sites More sharing options...
vchris Posted September 12, 2007 Author Share Posted September 12, 2007 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 More sharing options...
Synook Posted September 12, 2007 Share Posted September 12, 2007 Every page that uses session variables needs, at the very top of the page session_start(); Link to comment Share on other sites More sharing options...
justsomeguy Posted September 12, 2007 Share Posted September 12, 2007 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 More sharing options...
vchris Posted September 12, 2007 Author Share Posted September 12, 2007 Ah that's what I was missing. Do I have to close the session at the end of the page as well or is that not required? Link to comment Share on other sites More sharing options...
justsomeguy Posted September 12, 2007 Share Posted September 12, 2007 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.