rondog Posted September 1, 2009 Share Posted September 1, 2009 Hi, I was wondering how to go about restricting a users login to once per week? My idea is, make a lastLoggedIn field on the table and test against that every time they try and login if currentDate - lastLoggedIn date is less than a week, deny access. Does that sound about right? Is their an easier solution? Quote Link to comment Share on other sites More sharing options...
p2grace Posted September 1, 2009 Share Posted September 1, 2009 Sounds right to me Quote Link to comment Share on other sites More sharing options...
akitchin Posted September 1, 2009 Share Posted September 1, 2009 this is pretty much the most straightforward method - i can't think of anything that would be more efficient. if you want a strict weekly quota (that is, they can log in once per week, every week, rather than forcing a week between logins), you should use WEEK(). this would be useful in the scenario that the user logged in Friday, but should be allowed to log in the following Monday as it is a new week. if you're comparing the straight difference between current date and login date, they won't be allowed back in until next Friday. by using WEEK(), you can boil the dates down to which week they are in. Quote Link to comment Share on other sites More sharing options...
rondog Posted September 1, 2009 Author Share Posted September 1, 2009 this is pretty much the most straightforward method - i can't think of anything that would be more efficient. if you want a strict weekly quota (that is, they can log in once per week, every week, rather than forcing a week between logins), you should use WEEK(). this would be useful in the scenario that the user logged in Friday, but should be allowed to log in the following Monday as it is a new week. if you're comparing the straight difference between current date and login date, they won't be allowed back in until next Friday. by using WEEK(), you can boil the dates down to which week they are in. Oh that is an important question to ask my client. I guess I should clear that up before I get any further..thanks guys. Quote Link to comment Share on other sites More sharing options...
rondog Posted September 11, 2009 Author Share Posted September 11, 2009 Hey akaitchin if you see this, you were right about what the client wanted...say a user logs in on wednesday, they shouldnt be able to login until the following monday...so monday would be the start of the week rather than an actual week from login to login...I've never used WEEK() before. Heck i've never really used anything besides SELECT, INSERT, DELETE and UPDATE. Can you give a brief example of how I would use WEEK in this kind of situation? thanks man Quote Link to comment Share on other sites More sharing options...
akitchin Posted September 18, 2009 Share Posted September 18, 2009 what you would do is store the date of login as a DATE or a DATETIME field type. then, you can use WEEK() on that column to determine which week that last login was, and compare it with which week you're currently in: SELECT stuff FROM table WHERE WEEK(last_login_field) < WEEK(NOW()) this would select all rows where the `last_login_field` occurred during a week prior to the current. Quote Link to comment Share on other sites More sharing options...
shane18 Posted September 18, 2009 Share Posted September 18, 2009 Try: 1. Log the timestamp of the persons last login. 2. Figure out how many seconds are in a week. 3. Make it so if there (last login timestamp + a week in seconds) is < current timestamp they can login. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 18, 2009 Share Posted September 18, 2009 $user = mysql_real_escape_string($_POST['username']); $user = mysql_real_escape_string(md5($_POST['password'])); $sql = mysql_query("SELECT * FROM `table_name` WHERE DATE_SUB(CURDATE(), INTERVAL 1 WEEK) > `last_login_date` AND `user_name` = '$user' AND `password` = '$password'"); if(mysql_num_rows($sql)==1){ $row = mysql_fetch_array($sql); echo 'Welcome '.$row['username']; }else{ // User can't log in echo 'User doesn\'t exist or you can\'t log in yet'; } Quote Link to comment Share on other sites More sharing options...
akitchin Posted September 18, 2009 Share Posted September 18, 2009 neither of the above posts will work for the scenario proposed, as it will force a week's difference between logins (rather than one login per calendar week). try the query using WEEK() and see if it tickles your fancy (ie. solves your problem). Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 18, 2009 Share Posted September 18, 2009 what you would do is store the date of login as a DATE or a DATETIME field type. then, you can use WEEK() on that column to determine which week that last login was, and compare it with which week you're currently in: SELECT stuff FROM table WHERE WEEK(last_login_field) < WEEK(NOW()) this would select all rows where the `last_login_field` occurred during a week prior to the current. that a master at work nice code... Quote Link to comment Share on other sites More sharing options...
ozestretch Posted September 19, 2009 Share Posted September 19, 2009 SELECT stuff FROM table WHERE WEEK(last_login_field) < WEEK(NOW()) On a side note.. can MONTH() (if it exists??) be used the same way? Quote Link to comment Share on other sites More sharing options...
akitchin Posted September 19, 2009 Share Posted September 19, 2009 SELECT stuff FROM table WHERE WEEK(last_login_field) < WEEK(NOW()) On a side note.. can MONTH() (if it exists??) be used the same way? indeed it can. in fact, MySQL has a whole series of handy functions for dealing with data, the only stipulation for their use being that you choose the appropriate data type for the given column: MySQL 5.1 Function reference for this thread in particular, have a look at 11.6: Date & Time Functions. Quote Link to comment Share on other sites More sharing options...
ozestretch Posted September 19, 2009 Share Posted September 19, 2009 for this thread in particular, have a look at 11.6: Date & Time Functions. Yarr be a wee scallywag but amiss yee not ie: thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.