• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

Display the login ID of the user

Trying to display the current login ID of a user.  Here is the code I'm using.  I'm very new to coldfusion so be easy on me.

Here is the error:

The DSN parameter to the getid function is required but was not passed in.

The error occurred in C:\ColdFusion8\wwwroot\myMobi\login\member\coupon.cfm: line 57

55 : <p>Display ID</p>
56 :
57 : <cfinvoke component="#Application.cfcRoot#.coupon" method="getid" returnvariable="get.id">
58 : <cfoutput>
59 : #get.id#


Here is the component:

<cffunction access="public" name="getID" output="false" returntype="numeric" roles="Member" displayname="get Security ID" hint="It returns the securityID.">
<cfargument required="true" name="dsn" type="string"/>
<cfargument required="true" name="username" type="string"/>
<cfquery name="getID" datasource="#arguments.dsn#">
SELECT ID
FROM SECURITY
WHERE USERNAME = <cfqueryparam value="#arguments.username#" cfsqltype="CF_SQL_VARCHAR" maxlength="50"/>
</cfquery>
<cfreturn getID.ID/>
</cffunction>

Any help would be greatly appreciated.
0
sonicimpulse
Asked:
sonicimpulse
  • 18
  • 15
1 Solution
 
danrosenthalCommented:
The function requires you to pass in a variable called "DSN".

So, this line:
<cfinvoke component="#Application.cfcRoot#.coupon" method="getid" returnvariable="get.id">

Should look like this insead:
<cfinvoke component="#Application.cfcRoot#.coupon" method="getid" returnvariable="get.id" DSN="Enter your DSN here">

0
 
sonicimpulseAuthor Commented:
No I get the same error but it says the username is not passed in.  Thanks for your help so far
0
 
danrosenthalCommented:
username is also required.
<cfinvoke component="#Application.cfcRoot#.coupon" method="getid" returnvariable="get.id" DSN="Enter your DSN here" username="Enter your Username here">
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
sonicimpulseAuthor Commented:
Thanks for all your help so far.  I understand where I was making my mistake and finally starting to understand this a little more.  

username="Enter your username here"

I dont' know what the username is.  It depends on who is log in to the site.  There could be more then 1 person log in at a time.  Thats what the query command is suppose to figure out for me.  Its supposed to give me their ID number.  What would I put there sense its unknown?
0
 
danrosenthalCommented:
How do you know a user is logged in? When you answer this question you will be able to answer the username question.
0
 
SidFishesCommented:
<cffunction access="public" name="getID" output="false" returntype="numeric" roles="Member" displayname="get Security ID" hint="It returns the securityID.">
<cfargument required="true" name="dsn" type="string"/>
<cfargument required="true" name="username" type="string"/> <<<<< this is where the error is being thrown

the cfargument tag provides a method for cffunction to do a kind of validation, in this case if you don't pass the username variable, an error is thrown. So this is an error "by  design". Your function need a username and a DSN for it to work. to see how this works, you can remove the 2 cfarguments and you won't get the error, you'll get ANOTHER error, specifically an error stating the arguments.dsn not found as the query won't have the parameters it needs.

You need to provide the dsn and user name for the function to work

 <cfinvoke component="#Application.cfcRoot#.coupon" method="getid" returnvariable="get.id">
<cfinvokeargument name="username" value="#somevalue#>
<cfinvokeargument name="DSN" value="#myDSN#>
</cfinvoke

note: you can write it as Dan has

<cfinvoke component="#Application.cfcRoot#.coupon"
                method="getid"
                returnvariable="get.id"
                DSN="#MyDSN#"
                username="#someUsername#">

but i find it's easier to read using cfinvokeargument

now... you do need to figure out how to set username and dsn


something like this would work

<cfif isdefined('form.username')>
 <cfinvoke component="#Application.cfcRoot#.coupon" method="getid" returnvariable="getid"> <<<<change here
<cfinvokeargument name="username" value="#somevalue#">
<cfinvokeargument name="DSN" value="myDSN">
</cfinvoke>

<cfoutput>
getID.ID
</cfoutput>

</cfif>

<form method=post" action="test.cfm">
<input type="text" name="userName">
<input type="submit">
</form>


















0
 
sonicimpulseAuthor Commented:
I'm assuming thats in the application.cfm file.  Here is what the file looks like.  I tried using a bunch of different variables but I continue to get the same error.

<cfsetting enablecfoutputonly="yes">
<!--- First declare the application and manage whitespace --->
<cfapplication name="securityf" clientmanagement="yes" sessionmanagement="yes" setclientcookies="yes" clientstorage="clientVariables">


<!--- Declare Application variables --->
<cflock scope="Application" timeout="10" type="Exclusive">
    <cfparam name="Application.dsn" type="string" default="security">
    <cfparam name="Application.webRoot" type="string" default="/myMobi/login/"><br />
    <cfparam name="Application.machineRoot" type="string" default="/myMobi/">
    <cfparam name="Application.cfcRoot" type="string" default="/myMobi/login/com">
</cflock>

<!--- Cflogin runs before anything else if the user is
      not authenticated. --->
<cflogin>
<!--- the cflogin scope comes from a login form named cflogin.
  If it      exists authenticate the user. --->
     <cfif isDefined('cflogin')>
            <cfscript>
                  // create the Security object
                  Security = createObject("component","#Application.cfcRoot#.security");
                  // store authenticate method of security object as roles variable.
                  variables.roles=Security.authenticate(cflogin.name,cflogin.password);
            </cfscript>
            <cfif variables.roles NEQ 0>
                  <cfloginuser name="#cflogin.name#" password="#cflogin.password#" roles="#variables.roles#">
            </cfif>
     </cfif>
</cflogin>

<!--- Check for command to log out the user. --->
<cfif isDefined("url.logout") AND url.logout>
     <cflogout>
</cfif>

<!--- this header is for the purposes of this demonstration only
      it is generally not a good idea to call content from either
--->
<cfinclude template="#Application.webroot#_dsp/dsp_header.cfm">

0
 
danrosenthalCommented:
I haven't used CFLOGIN myself, but my guess would be that the username is stored in the variable #cflogin.name#.
0
 
sonicimpulseAuthor Commented:
When I use that I get an error that says "name is undefined in cflogin"
0
 
danrosenthalCommented:
Sounds like the login process has not happened when you try to run the lookup to get ID of the user that has logged in. Make sense?
0
 
sonicimpulseAuthor Commented:
I got this at the top of the page to check to see if a user is log in.

<!--- First handle security.  if the user is not Authenticated
      get the login page --->

<cfif not len(getAuthUser())>
      <cfinclude template="#Application.webroot#security/login.cfm">
</cfif>
0
 
danrosenthalCommented:
You should wrap that same logic around the getID check because it doesn't make sense to check the ID of a user before they are logged in.
0
 
sonicimpulseAuthor Commented:
I'm trying to get the ID because I need to put that number into a different table.  I'm confused by what you said.
0
 
danrosenthalCommented:
Basically you are attempting to simply lookup in the database the userID of a given username.
Therefore, if you do not have the username it follows that you will not have any success translating it into a userID.

Does that clear anthing up?

0
 
sonicimpulseAuthor Commented:
Exactly so how do I get the username of the person log in.
0
 
sonicimpulseAuthor Commented:
Is there away to store the username in a variable when they log in. Then I can call it on a different page.  
0
 
danrosenthalCommented:
Usually I store the userID and a hash of the password in a cookie and authenticate the session in the application.cfm file on every page load. Then I store the userID in a variable that is globally available.

It sounds like your login process is not complete if you are not authenticating the user on every page load.
0
 
sonicimpulseAuthor Commented:
I have this on every page that checks if a user is log in.

I got this at the top of the page to check to see if a user is log in.

<!--- First handle security.  if the user is not Authenticated
      get the login page --->

<cfif not len(getAuthUser())>
      <cfinclude template="#Application.webroot#security/login.cfm">
</cfif>

0
 
sonicimpulseAuthor Commented:
This really can't be this hard to do.  All I'm trying to do is get the username of a person thats login the system.  Then I can display there name, get there id and so on with that information.  At this point how do I store the username in a variable.
0
 
danrosenthalCommented:
What happens when you run this?
<cfoutput>Authorized user: #getAuthUser()#</cfoutput>
0
 
sonicimpulseAuthor Commented:
Awesome that does work and gives me the username.  Now I need to figure out how to use that and get the ID of the username.
0
 
danrosenthalCommented:
Excellent. Now try something like this:


<CFSET variables.username = getAuthUser()>
<CFSET variables.dsn = "ENTER YOUR DSN HERE"
 
<cfinvoke component="#Application.cfcRoot#.coupon" method="getid" returnvariable="getid.id" DSN="#variables.dsn#" username="#variables.username#">
 
<CFOUTPUT>#getid.id#</CFOUTPUT>

Open in new window

0
 
sonicimpulseAuthor Commented:
Back to getting my original error:
 The USERNAME parameter to the getid function is required but was not passed in.

0
 
danrosenthalCommented:
Take a look at the result of this code:


<CFSET variables.username = getAuthUser()>
<CFSET variables.dsn = "ENTER YOUR DSN HERE">
 
<CFOUTPUT> 
cfinvoke component="#Application.cfcRoot#.coupon" method="getid" returnvariable="getid.id" DSN="#variables.dsn#" username="#variables.username#">
</CFOUTPUT>

Open in new window

0
 
sonicimpulseAuthor Commented:
Same error again.  Thanks for all your help so far on this.  Will get it working here soon.
0
 
danrosenthalCommented:
You shouldn't be getting an error from that code. Can you post the code and error message here?
0
 
sonicimpulseAuthor Commented:
Here is the error:
 The USERNAME parameter to the getid function is required but was not passed in.
 
The error occurred in C:\ColdFusion8\wwwroot\myMobi\login\member\coupon.cfm: line 62

60 :  
61 : <CFOUTPUT>
62 : <cfinvoke component="#Application.cfcRoot#.coupon" method="getid" returnvariable="getid.id" DSN="#variables.dsn#" username="#variables.username#">
63 : </CFOUTPUT>


Here is the code:
<CFSET variables.username = getAuthUser()>
<CFSET variables.dsn = "security">
 
<CFOUTPUT>
<cfinvoke component="#Application.cfcRoot#.coupon" method="getid" returnvariable="getid.id" DSN="#variables.dsn#" username="#variables.username#">
</CFOUTPUT>
0
 
sonicimpulseAuthor Commented:
Here is a copy of the component:
      <cffunction access="public" name="getID" output="false" returntype="numeric" roles="Member" displayname="get Security ID" hint="It returns the securityID.">
            <cfargument required="true" name="dsn" type="string"/>
            <cfargument required="true" name="username" type="string"/>
            <cfquery name="getID" datasource="#arguments.dsn#">
             SELECT ID
             FROM SECURITY
             WHERE USERNAME = <cfqueryparam value="#arguments.username#" cfsqltype="CF_SQL_VARCHAR" maxlength="50"/>
              </cfquery>
            <cfreturn getID.ID/>
      </cffunction>      
0
 
danrosenthalCommented:
aha...you modified my code. I purposely left off the opening "<" so that we would see what variables are being passed into the component.

0
 
sonicimpulseAuthor Commented:
OK here is what it says when I take off the <

Authorized user: sonicimpulse cfinvoke component="/myMobi/login/com.coupon" method="getid" returnvariable="getid.id" DSN="security" username="sonicimpulse">
0
 
danrosenthalCommented:
I am not sure. I suggest modifying the function to simply return a hard-coded number and require no input variables and assuming that works add back in elements until you determine the actual problem.

I would start with something like this:
      <cffunction access="public" name="getID" output="false" returntype="numeric" roles="Member" displayname="get Security ID" hint="It returns the securityID.">
            <CFSET returntest = "10634"/>
            <cfreturn returntest/>
      </cffunction>      

Open in new window

0
 
sonicimpulseAuthor Commented:
I tried this.  I'm not getting any error but its not displaying the data:

<CFSET variables.vusername = getAuthUser()>
<CFSET variables.dsn = "security">

<cfquery name="getID" datasource="#variables.dsn#">
             SELECT MEMBERID
             FROM SECURITY
             WHERE USERNAME="#variables.vusername#"
              </cfquery>
             <cfreturn getID/>
<cfoutput>
#getID#
</cfoutput>
0
 
danrosenthalCommented:
This is the correct format:


<CFSET variables.vusername = getAuthUser()>
<CFSET variables.dsn = "security">
 
<cfquery name="getID" datasource="#variables.dsn#">
             SELECT MEMBERID
             FROM SECURITY
             WHERE USERNAME="#variables.vusername#"
</cfquery>
 
<cfoutput>
#getID.MemberID#
</cfoutput>

Open in new window

0
 
sonicimpulseAuthor Commented:
Yes it was.  Thanks so much for all of your help
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 18
  • 15
Tackle projects and never again get stuck behind a technical roadblock.
Join Now