Gossamer Forum
Home : Products : DBMan : Installation :

Write to db from create account

(Page 1 of 2)
> >
Quote Reply
Write to db from create account
How do I write to the database from the create an account screen?
Let's say that I want to add form field to the create an account page. Already present is form fields for userid, password and email. Let's say I want to add another field called User Type: and the field is html defined as <input name=usertype value="" ...>

How do I capture the value and write it to the database at this point. (I know that the other 3 fields are written to the db.pass file.
Quote Reply
Re: Write to db from create account In reply to
You really need to have all the database fields in the form you use to write to the database. Is your database going to be just

Username
Password
Email
UserType

If so, you can do it. Otherwise, you should think of another way to accomplish what you want your end result to be.


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





Quote Reply
Re: Write to db from create account In reply to
I suppose there are several options:
1. setup all of my db record fields as hidden except the usertype or
2. pass the value of user type to a temporary file -- much like storing the value of userid in the db.pass file for later writing to the record when a new user adds a record for the first time or
... ?

With the first option, can I accomplish that from the signup (create) account page?

Option 2 would probably require major modifications to the db.cgi file.
Any other ideas?
Quote Reply
Re: Write to db from create account In reply to
Is it really *that* important the the user fills out everything at once? Wouldn't it be acceptable for the user to fill out the login information, log in and then go automatically to the add form for the database?


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





Quote Reply
Re: Write to db from create account In reply to
I don't want the user to add everything at once.

I want them to create an account but I want to identify the category of user. That is, some users will have more privledges than others. So by creating a USERTYPE when the user creates a logon id and password and then writing the usertype value to the record will enable me later to retrieve the type of user from the record and do various conditional tests for that user.

It's really a simple question. How do I write a value from the create or signup page to the record. To make this less confusing, as an example, lets say I have a time field in my record and I want to write the time that the user creates the record. How would the time value get written to the record from the create/signup sub. (I'm not using time but usertype which will be input along with userid, password and email).
Quote Reply
Re: Write to db from create account In reply to
I suppose, then, that the best thing would be to add a field to your password file.

How will you know what the user type is?



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





Quote Reply
Re: Write to db from create account In reply to
OK -- I've got things set up so that I can write my usertype to the password file. It works great and I have made the appropriate mods to the admin sub for updating userid, password, email and now user type. All that works well.

I'm a bit confused about the next step:
I want to write the usertype to the database record at the same time the userid is written to the record and in the same manner as in sub add record in the db.cgi file. The problem is trying to figure out how to get the usertype from the password file and use it to add to the record. I've tried to follow, by analogy, how the userid is written but I get confused with uid, db_userid and userid. (I could do what you've done with the email lookup but it seems like overkill for this issue).
I just need to know how, at logon, the userid is validated and made available to the script -- seems like I could retrive my new usertype at the same time. Any suggestions will be appreciated.
Quote Reply
Re: Write to db from create account In reply to
How are you using the usertype in your script? Do you have a variable that is set when the user logs in, similar to the $per_add, $per_del, $per_admin, etc variables?


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





Quote Reply
Re: Write to db from create account In reply to
I pass a value to the create an account sub (from outside dbman). For example I have a link in a non-dbman html page:
http://....../.db?&short=1&signup=1

Here's the relevant code in the signup page of dbman:

if ($in{'short'}) {
$db_acct = "short"; }
else {
$db_acct = "long"; }

print qq|
<form action="$db_script_url?&short=$db_acct" method="post" name="form1">
<input type=hidden name="db" value="$db_setup">
<input type=hidden name="uid" value="$db_uid">
<input type=hidden name="user_type" value="$db_acct">

The db.cgi then writes the value of user_type to the password file along with the userid, password, permissions, and email.

This is all working fine.

Now the issue is when a user logs on the first time my setup is to go directly to the add a record form. It's at this point I want the user_type written to the record along with the userid.
How do I get the value of $user_type into the record?
In my db.cgi I have the following in the add sub but it doesn't work.

# Set the userid to the logged in user.
($auth_user_field >= 0) and ($in{$db_cols[$auth_user_field]} = $db_userid);
($auth_db_acct >= 0) and ($in{$db_cols[$auth_db_acct]} = $user_type);

Note that my cfg file is setup for the $auth_db_acct stuff above.

Thanks

[This message has been edited by pabloa (edited July 07, 1999).]

[This message has been edited by pabloa (edited July 07, 1999).]
Quote Reply
Re: Write to db from create account In reply to
Couldn't you just read the value into a variable and use that when they log in? Since the value is in the password file, you'd just need to read the value just like the $per_add, $per_admin, etc. variables are read. Call it, as you have before, $db_acct. Then in your add form, use $rec{'usertype'} =$db_acct;


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





Quote Reply
Re: Write to db from create account In reply to
Your suggestion seems to make sense. Where do I read in the various permissions? Is that in the start of the db.cgi file with the statement: ($status, $uid, $per_view, $per_add, $per_del, $per_mod, $per_admin)
= &auth_check_password; # Authenticate User, get permissions and userid.
Would I then just add $db_acct to the above?

Do I also need to pass the $db_acct to the sub main ?
Quote Reply
Re: Write to db from create account In reply to
Yep. I would add

$db_acct to the list of variables.

Also, you'll need to add it to the auth.pl script.

You'll see a line in sub auth_check_password:

Code:
($userid, $pw, $view, $add, $del, $mod, $admin) = split (/:/, $pass);

I'm assuming that your password file has the usertype after the admin permission, so just change it to read

Code:
($userid, $pw, $view, $add, $del, $mod, $admin, $acct) = split (/:/, $pass);

A little further down, you'll see

Code:
return ('ok', $db_uid, $view, $add, $del, $mod, $admin);

change that to

Code:
return ('ok', $db_uid, $view, $add, $del, $mod, $admin, $acct);

In auth_check_permissions, change

Code:
($name, $pw, $view, $add, $del, $mod, $admin) = split (/:/, $permission);

to

Code:
($name, $pw, $view, $add, $del, $mod, $admin, $acct) = split (/:/, $permission);

and change

Code:
return ($view, $add, $del, $mod, $admin);

to

Code:
return ($view, $add, $del, $mod, $admin, $acct);

That's it. From then on you can use $db_acct as a variable to apply to the user, just like you use the other variables.


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





Quote Reply
Re: Write to db from create account In reply to
Thanks -- this is good and I've tested it so far and it works well to a point. I've tested the retrieval from the password database and through the auth.pl routine and it all works.

The one remaining problem shows my perl ignorance -- how and where do I set up a global variable? Like $db_acct above?

Thanks for all your help
Quote Reply
Re: Write to db from create account In reply to
This is really baffling! I have made all the changes in the auth.pl file. I've substituted $user_type for your $acct.

In the sub main the lines are now:
($status, $uid, $per_view, $per_add, $per_del, $per_mod, $per_admin, $db_user_type) = &auth_check_password; # Authenticate User, get permissions and userid.

Then after the above I put some test code:
if ($db_user_type eq "short" {
exit;
}

I logged on with a user's id who had usertype short in the password file -- the above code worked and I exited db.cgi. I then logged on with a user's id who had usertype long in the password file -- the above code worked and I could continue on in dbman.

But when I try to use $db_user_type in the html.pl file it doesn't work.
My debug code print "--$db_user_type--"; yields ----- . My conditional tests using the $db_user_type doesn't work. Any ideas?


[This message has been edited by pabloa (edited July 08, 1999).]
Quote Reply
Re: Write to db from create account In reply to
Update to above message:

Here's the latest:
1. I can get the value from $db_user_type the first time I logon. The value of this variable is present.
2. I seem to loose the value depending on what I do. For example. If I logon the first time, the $db_user_type has a value and this carries over to say add a record. But if I go to modify record, I loose the value of $db_user_type.
3. I can't get the value of $db_user_type to write into my record. (It's in the pw file but I want it be just like userid -- both in the password file and the record. I've tried adding the line to the add sub in the db.cgi file: # Set the userid to the logged in user.
($auth_user_field >= 0) and ($in{$db_cols[$auth_user_field]} = $db_userid);
($auth_db_acct >= 0) and ($in{$db_cols[$auth_db_acct]} = $db_user_type); along with the following update to my home html section of the html.pl file: unless ($per_admin) { $in{'Userid'} = $db_userid; $in{'Short'} = $db_user_type; my ($status, @hits) = &query("mod"); undef %in; unless ($status eq 'ok') { &html_add_form; return; }}

The above combination does not write the value to the record.
4. I've tried in the record html form the following: input name=usertype value="$rec{'usertype')= $db_user_type .... no luck.

So any ideas on 1. retaining the value of my $db_user_type variable across tasks without relogging on and 2. how to get the value of $db_user_type written to the record which would help me eliminate the problem with number 1.
Thanks
Quote Reply
Re: Write to db from create account In reply to
 
Code:
<input type="hidden" name="usertype" value="$rec{'usertype'}">



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





Quote Reply
Re: Write to db from create account In reply to
All variables are global unless they are defined otherwise by usiing my or local.

Glad it worked! Smile

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





Quote Reply
Re: Write to db from create account In reply to
Well -- almost worked! Again my perl ignorance is probably at fault -- here's where I'm at:

With the above mods to the auth.pl and the sub main in the db.cgi script, I can test the returned value of $user_type from auth.pl and see the content of $user_type. But when I want to use the value of this variable in my html.pl file, I can't see it or I'm doing something wrong.

Here's an example from the &hml_record_form:

The code is basically my form but I want to test the value of $user_type to see what kind of field will display, for example:

 </TD>|; if ($user_type eq "short") { print qq|
<TD WIDTH=411>

<P><INPUT id="FormsEditField61" TYPE="text" NAME="Region" VALUE="$rec{'Region'}" SIZE=40 MAXLENGTH=255 onfocus="this.blur()">                   & nbsp; <FONT SIZE="-2" FACE="verdana">Indicate geographic region, e.g. gold country or New England, etc.</FONT></TD>|; }
print qq| </TR> and so on.

The if test on the $user_type doesn't work. Shouldn't $user_type be a global variable? I can test it's value in db.cgi sub main and it's fine -- where am I going wrong here?




[This message has been edited by pabloa (edited July 08, 1999).]
Quote Reply
Re: Write to db from create account In reply to
I see you're using a different variable name than I suggested. Smile That's okay, but I need to know where you have it. Is it every place where I used $acct?

Did you also include your variable in sub main with the

($status, $uid, $per_view, $per_add, $per_del, $per_mod, $per_admin) = &auth_check_password; # Authenticate User, get permissions and userid.

lines?

You say that the variable shows up in sub main, right? You might try using something like

$db_usertype = $usertype;

around where $db_userid is defined. Then use $db_usertype in your form.

This is all guesswork on my part -- basic trial and error. Smile

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





Quote Reply
Re: Write to db from create account In reply to
Wow this is quite a discussion.
In response to your questions:

quote:
--------------------------------------------------------------------------------

if I go to modify record, I loose the value of $db_user_type.

--------------------------------------------------------------------------------

Is it that the $db_user_type variable doesn't give a result or that the result isn't written to the record?

Try just printing $db_user_type on your modify form.

Both -- the $db_user_type is not written to the record and although I can retrieve it from the password file with the prior mods, it prints out ---- on the modify form -- but only if I have previously logged on and done some else first, for example added a record and then click on modify to make changes. If I logon and go directly to modify, it works.


quote:
--------------------------------------------------------------------------------

($auth_db_acct >= 0) and ($in{$db_cols[$auth_db_acct]} = $db_user_type)

--------------------------------------------------------------------------------

Do you have a hidden field on your form that corresponds to the $auth_db_acct field number? Is that your usertype field?

Yes -- why doesn't that code fragment work? The parallel code for userid works!


quote:
--------------------------------------------------------------------------------

unless ($per_admin) { $in{'Userid'} = $db_userid; $in{'Short'} = $db_user_type; my ($status,@hits) = &query("mod"); undef %in; unless ($status eq 'ok') { &html_add_form; return;
}}

--------------------------------------------------------------------------------

I'm not sure why you use the $in{'Short'} = $db_user_type here. You'll get the same results without it. All this does is switch to the add form if there's not a record for the user, right?


Yes, I don't need this and it has been deleted.

quote:
--------------------------------------------------------------------------------

I've tried in the record html form the following: input name=usertype value="$rec{'usertype')= $db_user_type .... no luck.

--------------------------------------------------------------------------------

This may be your problem. Since you're defining the user_type within sub add_form, this should just be


code:
--------------------------------------------------------------------------------

<input type="hidden" name="usertype" value="$rec{'usertype'}">

--------------------------------------------------------------------------------
Where does usertype come from to be written to the record? How do I get the value of usertype which is now in the password file into the record.

In summary, I've obtained $db_user_type from the db.cgi script which reads usertype from the password file into $db_user_type.

How do I get $db_user_type to write to the record at the same time the userid is written for the first time.


Quote Reply
Re: Write to db from create account In reply to
You can either use the code I gave to eskimoz in the other thread, or you can just add the following to sub get_defaults.

After

Code:
foreach $field (keys %db_defaults) {
$default{$field} = $db_defaults{$field};
}

add

Code:
$default{'usertype')= $db_user_type;


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





Quote Reply
Re: Write to db from create account In reply to
OK, I think we are almost there ....?

I've tried all kinds of versions of our discussion. Here are some findings:

1. As stated before, I can always write my usertype to the password file and retrieve it in the admin sub and logon routines.
2. I can now get the value of usertype to display in the add form using value="$rec{'usertype'}$db_user_type" or by using the following statement $rec{'usertype'} = $db_user_type; at the beginning of the add form and then using value="$rec{'usertype'} in the body of the add form.
3. The first method above $rec{'usertype'}$db_usertype displays in the form but doesn't save the value when added. The second option above works: set the value of $rec{'usertype'} before the print qql for the form. But .....
using either of the above methods does not allow me to display the value of $rec{'usertype'} in the form when modifying or other - -- because we've set up $rec{'usertype'} to be equal to $db_user_type which is the value retrieved from the password file. Ordinarily this wouldn't be a problem but ....

5. The way I retrieve the value of usertype ($db_user_type) from the password file is the routine called from db.cgi which is located in auth.pl. This is a time sensitive routine. That is, if you just signed in and got your id, pw and usertype things are fine. But if you now leave dbman for a few minute and come back and relog on, then you cannot retrieve the value of usertype from the password file. This is because, the logon first goes to the auth folder to see if a recent id with random number exists and if so, uses that rather than searching the pw file. This is OK (and really preferable to save resources in searching the pw file for re-logons) for me as long as I can retrieve the value of usertype from my record -- but the way it is set up, as described earlier, I can't get the value because of my equality statement above.
Any ideas for this workaround and we will have this problem solved!

[This message has been edited by pabloa (edited July 13, 1999).]
Quote Reply
Re: Write to db from create account In reply to
I really don't know what to tell you. I often feel like I'm trying to do brain surgery on a hidden skull. I can't see what I'm doing! Smile

I'm working on something else that might give me an idea for you. Hang on a little bit. I have it written out, but I have to create a database in order to check it out and I haven't got the database created as yet.

With any luck, I'll get to it today.


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





Quote Reply
Re: Write to db from create account In reply to
Still doesn't work!

I've tried the mod with correction which you posted on the other forum.

I've tried this last suggestion. Still can not get the usertype into the record.

There has been lots of discussion here and the other forum. Let me just recap where I am to make sure you understand where I am.

Summary:
I can write my usertype to the password file. I can retrieve the same in the administrative sub and can change the value the same as the userid, password and email.

Now all I want to do is write the value of usertype to the record. In admin sub the value of usertype is stored in $data[8].
So in your last suggestion in the other forum, I put in the code which opened the password file and had the clause default{'usertype'} = data[8]; as follows
open (PASS, "<$auth_pw_file") or &cgierr ("unable to open: $auth_pw_file.\nReason: $!"); if ($db_use_flock) { flock(PASS, 1) } @lines = <PASS>;close PASS;foreach $line (@lines) { if ($line =~ /^$db_userid:/) { @data = split /:/, $line;$default{'usertype'} = $data[8]; last; }}

Note this mod is made in sub get_defaults.

Nothing is written to the record.

[This message has been edited by pabloa (edited July 13, 1999).]

[This message has been edited by pabloa (edited July 13, 1999).]
Quote Reply
Re: Write to db from create account In reply to
I don't know. I'm really sorry, but I just don't know.


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





> >