Jump to content

Restrict login time


rondog

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 2 weeks later...

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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';
}

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.