so, when a user visits the site for the first time i want to show a dialog box, and when the user clicks "x" or "hide" i want开发者_JAVA百科 to hide it from that user forever.
this only works with cookies, right?
so, when the user clears his/her cookies he/she will see the dialog box again, next time they visit the site, i assume.
or is there a better/more common way to do this?
Can you store a property on the user? It can be false
by default and when you click the 'x' you can make an AJAX call that sets it to true
. Your dialog box could key off this property...
This way you wouldn't have to worry about the scenario where the user clears the cookies. Of course it will only work if you have user objects server side and it is storing extra data to overcome a very small problem, so it might not be goood practice. Just an idea.
No, nor should there be. Cookies are the only means for a site to retain information. Allowing a site to do anything more is a serious breach in security.
This is a fairly common approach to stored state.
Create a user_properties table add column "firstTimeDialog" (or isVirgin) as SMALLINT with default value of '1'. when user clicks box send AJAX call to server-side to change the value to 0. Load user properties with user information into the session on login and you a very simple way of checking to see if its a users first time.
Remember a users first time should be memorable, so be gentle.
Cookies are the correct answer to persistent state, but it's not a bulletproof solution.
What if a user visits your site on one computer, and then visits again from a different computer? They'll see the message twice. Usually in cases such as these I recommend rethinking your approach.
- What is the purpose of the dialog?
- Is there a reason the user shouldn't see it a second time?
- Can it be placed elsewhere in the page for the same/better effect?
If you really want to be nasty with cookies, you can look at evercookie. But please only use your powers for good or for awesome.
精彩评论