0

I want the users of my website to be able to chose which style do they want, hand-writted or basic. For this I have created two stylesheets and a select box, so that when you chose an option that style link is charged. As the website has many pages, I'm thinking about using a $_SESSION value, but how do I set one by default? Is there a better option?

Thanks for your answer!!

gj7bx
  • 17
  • 4

3 Answers3

2

Let's say that you want to store the chosen stylesheet in $_SESSION['style'], then you can set a default as such:

if( empty( $_SESSION['style'] )) {
    // Set $_SESSION['style'] to what you want as the default
}
Nick Coons
  • 3,682
  • 1
  • 19
  • 21
0

You can try:

$style = 'default';
if(!empty($_SESSION['style'])) {
  $style = $_SESSION['style'];
}

I think the better option is to go with cookies. Either cookies or sessions, you're still going to have to check whether or not that value is defined, if not, define it.

0

You might want to consider storing the user's selection in a cookie. This is easy using JavaScript. You can simply read from and write to document.cookie to do so. Also see this answer concerning reading a cookie.

Something along the lines of

function storeStyle(style) {
    var cookieSubStrings = [], cookies = readCookies();
    cookies.style = style;
    if (!("expires" in cookies))
        cookies.expires = (new Date().getTime() + 30*24*60*60*1000).toGMTString();
    for (e in cookies)
        if (cookies.hasOwnProperty(e))
            cookieSubStrings.push(e + "=" + cookies[e]);
    document.cookie = cookieSubStrings.join("; ")
}

var style, cookies;
cookies = readCookies();
if (!("style" in cookies)) {
    style = "basic";
    storeStyle(style);
} else {
    style = cookies.style;
}

will be sufficient. You can then use the style variable to load the appropriate style sheet.

Community
  • 1
  • 1
Cu3PO42
  • 1,403
  • 1
  • 11
  • 19