Forum Moderators: coopster

Message Too Old, No Replies

Using sessions to limit a user's choices

want to limit to once per day

         

knockeddown

4:25 pm on Mar 19, 2007 (gmt 0)

10+ Year Member



I'm trying to put together a site for my class where the first page lists hyperlinks of different types of pets: like dogs, cats, birds, rabbits, etc. The URL sends the choice to the second page where students can pick from a list of favorite names for the respective pet. I'd like to limit their ability to choose to one type of pet per day (so every 24 hours it resets). Say they pick dog today, they can still pick cat or bird, but they have to wait till tomorrow to pick dog again. I'm trying to do this with sessions rather than cookies but I'm not sure how to go about it. I'm sure I have to use some kind of If statement or other conditional. Seems like whenever I try, I just get the session rewriting over itself (if that makes sense?!?). Also, what about putting the sessions into a database? – is that possible? Anyway, here's what I have so far to get the session to correspond to the user's initial pet choice. Thanks for the help ya'll.

session_set_cookie_params (86400, '', '');
session_start();
$nameList = $_GET['pet'];
$_SESSION['nameList'] = $nameList;

joelgreen

5:10 pm on Mar 19, 2007 (gmt 0)

10+ Year Member



Check this link for "session duration". It looks like what you need.

[ua.php.net...]

eltreno

6:36 pm on Mar 21, 2007 (gmt 0)

10+ Year Member



The only way to truly do this properly would be to make user log in and record timestamp when they have made a selection.
Then let them choose again the next day.

Sessions
These are only good per browser session so once they close browser they can open it again and it's like they are starting from new.

Cookies
You can use cookies to flag they have made a choice and set cookie to last for 24 hrs in which case you can not allow them to choose again until next day arrives.
Problem here though is if users are not logged in, User A can choose a pet. Close browser log off, User B comes to same computer and logs in open browser whos cookie is say a pet was chosen within last 24 hrs so user B who didnt choose pet can't choose one.

Log in system is the only way to truly track this functionality across users and mulitple browser sessions.

Hope this helps
Trent

knockeddown

1:56 pm on Mar 23, 2007 (gmt 0)

10+ Year Member



Hey, thanks for the advice. Could anyone recommend a good book or tutorial that talks about tracking and storing session information in a database?

vincevincevince

2:01 pm on Mar 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just a tip from me...

Try to use cookies instead of sessions. Why? Sessions have all kinds of locking and URL change problems.

Just stick all your 'cookie-session' things into one array, and write it to file with file_put_contents("sessions/$cookievalue",$seralise($session));

joelgreen

2:30 pm on Mar 23, 2007 (gmt 0)

10+ Year Member



Note: file_put_contents available starting php5

knockeddown

5:26 pm on Mar 23, 2007 (gmt 0)

10+ Year Member



That sounds pretty neat. So you can actually use file_put_contents to add to the cookie as a user moves about a site? That's what it sounds like I'm hearing but I'm real new to this stuff. Thanks guys.