News:

Want to get involved in developing SMF? Why not lend a hand on our GitHub!

Main Menu

How to add a custom cookie to smf 2.0

Started by Sheda, September 05, 2011, 01:04:19 PM

Previous topic - Next topic

Sheda

Hello,
um... so, i wanted the forum to remember a member's theme, so it does not change everytime you log in or out. However, i've never made a cookie before and i ain't certain how to pull it off properly, without potentially messing up smf's native cookies.
Maybe someone did something similar already, or knows how to do it?
I don't necessarily need help on the theme part, as a friend of mine had some ideas which made sense to me (though of course, you are free to explain how it is done :D), it's more an issue of storing the cookie's information, alongside those that are already in place.

busterone

SMF already remembers individual user's preferences, including what theme they have selected.  It will display the forum default theme to guests, and once a member is logged in, their selected theme will be displayed.

Sheda

Yes, i already know that ;)
The thing is, some people may not want to be automatically logged into the forum everytime they visit (they might be sharing a computer with others, for example). I'm one of the people who likes to frequently log in and out, and i thought, changing the theme to anything but the default and then logging in and out on the forum, all the time having the theme change from the default to my chosen to the default theme again, might be annoying.
So i thought about saving the data which theme is used on the computer, independent from being logged in or not.

rawlogic

Sheda, you can do what I did, which was to select a default theme and disable the feature to let users choose their own theme. That way everybody has the same theme, and you don't have to worry about it.

If you plan on coding something, it's easy to handle cookies in PHP. Simply use the setcookie() function and the $_COOKIE superglobal.

Note that using cookies to store this will only work if someone accesses it on the same computer, with the same login, with the same browser.

rawlogic

Also, if you use a cookie to store and display the theme for logged out (unknown) users, on a shared computer, you're going to end up displaying the theme for the last user that visited the site (the last saved theme). So, you still might not get what you want. On a shared computer, would you rather see the default theme or the last user's theme? I'd rather see the default, which is what it does now.

If you're only worried about yourself, use your favorite theme as the default. If you really want to make things consistent, force the default on everybody. :)

Sheda

Quote from: rawlogic on September 08, 2011, 01:11:39 PM
Sheda, you can do what I did, which was to select a default theme and disable the feature to let users choose their own theme. That way everybody has the same theme, and you don't have to worry about it.
Of course, i could do this. But that's not the point. I want everybody to be able to choose a theme of their liking, one that fits their particular needs. At least 2 or 3 different themes should be available: A flashy one (default), a plain one (easier to read and navigate), some alternative in color and style and maybe one for a game featured on my website.
I'm starting slowly, with only one theme, but the idea is to make a variety and i wanna make it convenient for my forum members to have the site take on "their" style, without them having to do anything (login, for example).

Quote from: rawlogic on September 08, 2011, 01:11:39 PMIf you plan on coding something, it's easy to handle cookies in PHP. Simply use the setcookie() function and the $_COOKIE superglobal.
Yes, that's the theory... it's only i'm not sure yet how to use them and there are already cookies processed in $_COOKIE, so how do i make sure i ain't messing things up? Also Smf has quite a strange way to set their cookies. I haven't even found what they are called yet, the only thing i see written all over the place is $cookiename. Very informative.
If only some smf-savant could explain to me how the smf-cookies work and how to put my cookie alongside them... of course i could just try it by putting my own setcookie() into the code, but i'm afraid of bugs popping up xD
Please understand, i'm a beginner!

Quote from: rawlogic on September 08, 2011, 01:11:39 PMAlso, if you use a cookie to store and display the theme for logged out (unknown) users, on a shared computer, you're going to end up displaying the theme for the last user that visited the site (the last saved theme). So, you still might not get what you want. On a shared computer, would you rather see the default theme or the last user's theme? I'd rather see the default, which is what it does now.
This probably won't be much of an issue... my website is pretty... small ;)
I'd be surprised and happy, if one day more than 50 users are on it. The chances for people sharing a computer both being members of my page don't seem that high and if they are, they could still use the same theme or just disable the cookie ^^

Advertisement: