SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: Set default cookie
-
Jul 22, 2009, 08:48 #1
- Join Date
- Sep 2006
- Posts
- 105
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Set default cookie
HI all,
I'm working with a stylesheet switcher which sets the value of the new theme name inside a cookie. Then I'm pulling that value in through the link's href.
Is there an easy way to set a default value without having it overwrite the new cookie value on refresh? Currently my default value continually overwrites the chosen theme on page load.
In my header:
Code:<?php setcookie("newtheme", "theme_a", time()+3600); ?> <link rel="stylesheet" id="new-theme" type="text/css" href="/wp-content/themes/<? echo $_COOKIE["newtheme"]; ?>/css/style.css" />
-
Jul 22, 2009, 08:53 #2
- Join Date
- Jul 2008
- Posts
- 5,757
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:if (empty($_COOKIE['newtheme'])) {
// set a default
}
-
Jul 22, 2009, 09:05 #3
- Join Date
- Sep 2006
- Posts
- 105
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks for your reply!
So i've set:
Code:<?PHP if (empty($_COOKIE['newtheme'])) { setcookie("newtheme", "theme_a", time()+3600); } ?>
I'm using the jQuery cookie plugin to set the theme cookie on click.
Code:$.cookie('newtheme', themename, { path: '/', expires: 10 });
-
Jul 22, 2009, 09:13 #4
- Join Date
- Jul 2008
- Posts
- 5,757
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
check your cookies to make sure your javascript code is working as expected.
-
Jul 22, 2009, 09:15 #5
- Join Date
- Sep 2006
- Posts
- 105
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I've set an alert after setting the new theme to display the cookie value. It seems to be setting correctly
-
Jul 22, 2009, 09:23 #6
- Join Date
- Sep 2006
- Posts
- 105
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
opps - sorry it's working properly. I just had to clear my cookies.
Although, it does take 2 refreshes to set the default cookie. Any ideas why that why be?
-
Jul 22, 2009, 09:32 #7
- Join Date
- Jul 2008
- Posts
- 5,757
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
php's $_COOKIE array contains the values that were sent to it by the browser. setcookie() does not manually update the array, it just tells the browser to send a cookie the next time it makes an http request. so, $_COOKIE won't be updated until the next http request.
You can use a seperate variable which you assign either the default value, or the value from $_COOKIE, depending on whether or not $_COOKIE has the value you want.
-
Jul 22, 2009, 09:35 #8
- Join Date
- Sep 2006
- Posts
- 105
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
ahh i see
thanks a bunch!
Bookmarks