Gossamer Forum
Home : Products : DBMan : Customization :

Changing a .db file at login

Quote Reply
Changing a .db file at login
I would like to have my "user.db" file modified when a user logs in. The fields in the database I would like to update at login are:

Last login date: %get_date
Last IP address: $ENV{'REMOTE_ADDR'}

I haven't quite figured out how to do this yet, but I would like to also determine how many records are currently open for the user. This number needs to be calculated after the "sub auto_delete" mod. Perhaps this entire subroutine could be implemented after the delete mod is called.

Any help on this subject would be appreciated. Thanks! Smile

Frank


[This message has been edited by fharris (edited October 05, 1999).]
Quote Reply
Re: Changing a .db file at login In reply to
You would need to add the modify routine to the auth.pl file, sub check_password, after

Code:
foreach (0 .. 3) { $permissions[$_] = int($permissions[$_]); }

Basically, what you would add would be similar to sub modify_record in db.cgi.

What are the names of the fields you want modified?



------------------
JPD





Quote Reply
Re: Changing a .db file at login In reply to
Here are the fields I would like to modify at login:

UserRecordOpen
UserLastLogin
UserLastIP
UserLastDomain

Thanks,
Frank
Quote Reply
Re: Changing a .db file at login In reply to
This is another mod that will take some thought, so I probably won't be able to have it for you for a couple of days.


------------------
JPD





Quote Reply
Re: Changing a .db file at login In reply to
I think I got this worked out. This is something I'm trying to incorporate in a new application of DBMan for myself, so I actually did test it to make sure it worked.

In auth.pl, sub auth_check_password, after

$found=1;

add

Code:
&switch_to_user;
open(USER, "<$db_file_name") or &cgierr("unable to open user file: $db_file_name. Reason: $!\n");
@users = <USER>;
close USER;
open (USER, ">$db_file_name") or &cgierr("unable to open user file: $db_file_name. Reason: $!\n");;
if ($db_use_flock) {
flock(USER, 2) or &cgierr("unable to get exclusive lock on $db_file_name.\nReason: $!");
}
LINE: foreach $user (@users) {
if ($user =~ /^$/) { next LINE; }
if ($user =~ /^#/) { next LINE; }
chomp ($user);
@data = split (/\Q$db_delim\E/o, $user);

if ($data[$auth_user_field] eq $userid) {

# Add whatever other variables you want here.
# Use the syntax $data[field number] = variable
$data[6]=time();

print USER (join ('|',@data) . "\n");
}
else {
print USER $user . "\n";;
}
}
close USER; # automatically removes file lock

It worked for me! Smile

------------------
JPD





Quote Reply
Re: Changing a .db file at login In reply to
It worked great for me too! Smile

I have just one more question on this. How do I go about setting up a "UserRecordOpen" that determines the number of records open the user has (if any)?

Thanks again Smile
Frank
Quote Reply
Re: Changing a .db file at login In reply to
Where do you want to do this? Is this a variable you want to be able to use throughout DBMan? Or is it something you want to add to the user's record when the user logs in?


------------------
JPD





Quote Reply
Re: Changing a .db file at login In reply to
I answered my own question when I looked back over your previous posts. Smile (I thought I'd add a new post so I could see my previous code as I wrote this.)

Before the code I gave you yesterday, add

Code:
&switch_to_item;
open (DB, "<$db_file_name") or &cgierr("unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>; # Slurp the database into @lines..
close DB;

RECORD: foreach $line (@lines) {
if ($line =~ /^$/) { next RECORD; }
if ($line =~ /^#/) { next RECORD; }
chomp($line);
@data = split (/\Q$db_delim\E/o, $line);
if ($data[$auth_user_field] eq $userid) {
++$record_count;
}
}

Then, when you alter the user file, use

Code:
$data[7]=$record_count;

Something just occurred to me about the code I gave you yesterday. The first time a user logs in, you will not be able to save the login information in the record because there won't be a record in the database. I just thought I should mention that. It shouldn't cause a problem, though.


------------------
JPD





Quote Reply
Re: Changing a .db file at login In reply to
That worked perfect!

I did add a few lines of code, in case there weren't any records open:

if ($record_count < 1) {
$record_count = 0;
}

Also,

I added this before both pieces of code, to determine if the user exists in the "user.db" file before proceeding. I figure it's better safe than sorry, since I've lost my user.db file too many times now. hehe.

&switch_to_user;
%rec2 = &get_record($db_userid);

if (%rec2) {
.
.
}


Thanks,
Frank
Quote Reply
Re: Changing a .db file at login In reply to
Good idea! Smile

As I was writing the code for both of us yesterday I had a teeny little mistake that kept deleting all the records in my .db file. I know what you've been going through!

You're welcome!


------------------
JPD





Quote Reply
Re: Changing a .db file at login In reply to
I'd like to write the date to a field in the .DB file at login as well. I attempted to follow this thread but the changes eventually agreed upon called for the insertion of code after $found=1 and I have no such statement in my auth.pl file (due to one mod or another I suspect.)

Can you re-state the code and where to put it so that the field gets written to the user record when they log in? Thanks!!!

Tom


Quote Reply
Re: Changing a .db file at login In reply to
I don't seem to have $found=1; either. I don't know what I was referring to back then.

In sub auth_check_password, after

&auth_logging('logged on', $userid) if ($auth_logging);

add

Code:

open(DB, "<$db_file_name") or &cgierr("unable to open file: $db_file_name. Reason: $!\n");
@users = <DB>;
close DB;
open (DB, ">$db_file_name") or &cgierr("unable to open file: $db_file_name. Reason: $!\n");;
if ($db_use_flock) {
flock(DB, 2) or &cgierr("unable to get exclusive lock on $db_file_name.\nReason: $!");
}
LINE: foreach $user (@users) {
if ($user =~ /^$/) { next LINE; }
if ($user =~ /^#/) { next LINE; }
chomp ($user);
@data = split (/\Q$db_delim\E/o, $user);
if ($data[$auth_user_field] eq $userid) {
# Add whatever other variables you want here.
# Use the syntax $data[field number] = variable
$data[the number of your date field]=&get_date(time());
print DB (join ($db_delim,@data) . "\n");
}
else { print DB $user . "\n"; }
}
close DB;


JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Changing a .db file at login In reply to
Perfect! Thank you, thank you, thank you. That little bit of info not only did the deed but was a nice lesson for doing the same thing anywhere. Gracias JPD, eres el mejor!