logo4.gif (18K)  
 
overlap.gif (1K)home : technologies : coldfusion : add username and password script
 jigsawcf.gif (1K)
- home
  design
  technologies
  ideas
  hosting
  management"
  portfolio
  prices
  contact
spacer.gif (1K) overlap2.gif (1K)
  asp
  audio/video
  coldfusion
  databases
  javascript
  php
spacer.gif (1K) overlap2.gif (1K)






Following on from the script to populate a database of users with unique random usernames and passwords, we then needed a script to add new members to the database. This script needed to do two things:
1) generate unique usernames and passwords as suggested login details.
2) check any submitted usernames to ensure they were not in use prior to adding them to the database.
If you have any comments or improvements on this script please feel free to send them to leigh@rylands-internet-solutions.co.uk

add unique usernames script:

Please note, this script was designed to run within the context of a user area where the rights to run the script are determined by user levels stored within session variables. This aspect of the scripting has been removed for the purposes of clarity. You will need to take some steps such as placing in a password protected directory to ensure it is not abused.

This script should be saved as "addusername.cfm".

While we are happy for this script to be used we would like to hear from anyone using it. Also an acknowledgement/link would be appreciated, we will leave the nature of this to your discretion. Please leave the headers intact. Next month we will show how you can allow users to safely get reminders of their login details.

addusername.cfm >>

<!--
       ========== addusername.cfm ==========
This script was created by Rylands Internet Solutions, copyright 2002
This script maybe freely used as long as this header is left intact

This script is provided "as is", with no warranty intended or implied.
Rylands Internet Solutions accepts no responsibiity for any loss or
damages arising from the use of this script.
www.rylands-internet-solutions.co.uk
-->
<CFSET unerror = 0>
<CFIF IsDefined("FORM.username") AND IsDefined("FORM.password")>
   <CFQUERY NAME="usernamecheck" DATASOURCE="localserver">
   SELECT username
   FROM members
   WHERE username = '#FORM.username#'
   </CFQUERY>
   <!-- check if a username already in use, usernamecheck.RECORDCOUNT will be 0 if unique -->
   <CFIF usernamecheck.RECORDCOUNT EQ 0>
       <CFSET unerror = 0>
       <CFQUERY NAME="adduser" DATASOURCE="localserver">
       INSERT INTO members (username,password,realname,email)
       VALUES ('#FORM.username#', '#FORM.password#', '#FORM.realname#', '#FORM.email#')
       </CFQUERY>
   <CFELSE>
       <CFSET unerror = 1>
   </CFIF>
<CFELSE>
   <CFQUERY NAME="usernames" DATASOURCE="localserver">
   select username
   from members
   </CFQUERY>
   <CFSET USERARRAY = ARRAYNEW(1)>
   <CFSET COUNT = 1>
   <CFOUTPUT QUERY="usernames">
       <CFSET USERARRAY[#COUNT#] = #USERNAME#>
       <CFSET #COUNT# = #INCREMENTVALUE(COUNT)#>
   </CFOUTPUT>
   <CFSET UNIQUE = 0>
   <CFLOOP CONDITION="#unique# LESS THAN 1">
       <CFSET USERNAMECHAR1=#CHR(RANDRANGE(65, 90))#>
       <CFSET USERNAMECHAR2=#CHR(RANDRANGE(65, 90))#>
       <CFSET USERNAMECHAR3=#CHR(RANDRANGE(65, 90))#>
       <CFSET USERNAMENUMBER=RANDRANGE(100, 999)>
       <CFSET USERNAME = #USERNAMECHAR1#&#USERNAMECHAR2#&#USERNAMECHAR3#&#USERNAMENUMBER#>
       <CFSET PASSWORDCHAR1=#CHR(RANDRANGE(65, 90))#>
       <CFSET PASSWORDCHAR2=#CHR(RANDRANGE(65, 90))#>
       <CFSET PASSWORDCHAR3=#CHR(RANDRANGE(65, 90))#>
       <CFSET PASSWORDNUMBER=RANDRANGE(100, 999)>
       <CFSET PASSWORD = #PASSWORDCHAR1#&#PASSWORDCHAR2#&#PASSWORDCHAR3#&#PASSWORDNUMBER#>
       <CFLOOP FROM="1" TO="#usernames.recordcount#" INDEX="innerloop">
           <CFIF #USERNAME# IS #USERARRAY[INNERLOOP]#>
               <CFSET UNIQUE = 0>
           <CFELSE>
               <CFSET UNIQUE = 1>
           </CFIF>
       </CFLOOP>
   </CFLOOP>
</CFIF>
<HTML>
<BODY>
<CFIF IsDefined("FORM.username") AND IsDefined("FORM.password")>
   <CFIF unerror EQ 0>
       <H3 align=center>user added succesfuly</H3>
       <P align=center>do you wish to add another?</P>
   <CFELSE>
       <H3 align=center>error adding user</H3>
       <P align=center>please try again ...</P>
   </CFIF>
<CFELSE>

</CFIF>
<CFOUTPUT>

<FORM ACTION="addusername.cfm" METHOD="post">
<TABLE align=center>

<TR><TD>username:</TD>
<TD><input type="text" name="username" value="#username#" size="10" maxlength="20"></TD>
</TR>

<TR><TD>password:</TD>
<TD><input type="text" name="password" value="#password#" size="10" maxlength="20"></TD>

<TR><TD>user's name:</TD><TD>
<CFIF unerror EQ 0>
   <input type="text" name="realname" size="20" maxlength="50">
<CFELSE>
   <input type="text" name="realname" value="#FORM.realname#" size="20" maxlength="50">
</CFIF>
</TD></TR>

<TR><TD>email:</TD><TD>
<CFIF unerror EQ 0>
   <input type="text" name="email" size="20" maxlength="100">
<CFELSE>
   <input type="text" name="email" value="#FORM.email#" size="20" maxlength="100">
</CFIF>
</TD></TR>
<TR><TD colspan=2 align=center><input type="submit" value="add this user"></TD></TR>
</FORM>
</CFOUTPUT>

</TABLE>

</BODY>
</HTML>

coldfusion links
usernames
add username
server issues
script security

copyright 2008, Rylands Internet Solutions