Jump to content

[SOLVED] Checking for a max login time


dt_gry

Recommended Posts

Okay so I am wanting to auto logout people from my system but I want to make sure I am thinking about this the right way.

 

If I am going to check the length of time that a user has been logged in, I would have to take the time that I record in Active_Users mysql table subtract it from the current time and if it is greater than 60 then I would log them out. Correct? But I would have to load all the times in the DB table into an array if I am not mistaken.

 

Thanks Guys! ;D

Link to comment
Share on other sites

It sounds like you are wanting this to run on each login, I would advise against that and look up a Cron Job.

 

Also I believe with MySQL you can check a timestamp, basically what you would do is something like this:

 

<?php
// time plus 60 min
$timeP60 = time()+(60*60);
$sql = "SELECT * FROM active_users WHERE last_activity < " . $timeP60;

?>

 

Given that the time in your database is a timestamp, that should pull all records out that need to be "logged out".

Link to comment
Share on other sites

Oracle it is pretty easy, I think mysql 5 also supports queries that do that, I am not sure about mysql 4.

 

Anyhow the easy without getting into advanced sql is:

 

<?php
// time plus 60 min
$timeP60 = time()+(60*60);
$sql = "SELECT * FROM active_users WHERE last_activity < " . $timeP60;
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
    foreach ($row as $key => $val) {
           $keys[] = "`" . $key . "`";
           $vals[] = "'" . $val . "'";
    }
    
     $new_sql = "INSERT INTO tablename (" . implode(", " , $keys) . ") VALUES (" . implode(", ", $vals) . ")";
    mysql_query($new_sql);
    $keys = "";
    $vals = "";
}
?>

 

I am not 100% sure on the implode part, but pretty sure.

 

Hope that helps. I may get bored and post the SQL that can do this also.

Link to comment
Share on other sites

Oh and here it is with just MySQL:

 

http://dev.mysql.com/doc/refman/5.0/en/insert-select.html

INSERT INTO tbl_temp2 (fld_id)
  SELECT tbl_temp1.fld_order_id
  FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;

 

Your Code:

 

<?php
// time plus 60 min
$timeP60 = time()+(60*60);
$sql = "INSERT INTO tablename (`col1`, `col2`) SELECT col1, col2 FROM active_users WHERE last_activity < " . $timeP60;
mysql_query($sql);
?>

 

I am not 100% on that one, my syntax could be wrong, but yea. Just an alternative method, I also do not know if this works on MySQL 4.

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.