tags:

views:

118

answers:

3

Why does this not work? My welcome message, it just doesn't show up:

<p>Welcome <cfoutput>#Recordset1.UserID#</cfoutput>.</p>

The session variable on the login page I created is:

<cflock timeout=999 scope="Session" type="Exclusive">
    <cfset Session.IDUsers =''>
</cflock>

is this incorrect? On the index page where I'm trying to display my welcome message I have:

<cfquery name="Recordset1" datasource="cfGossip">
  SELECT *
  FROM   users
  WHERE  users.IDUsers = <cfqueryparam value="#Session.IDUsers#">
</cfquery>

I'm not sure if this works, or is necessary?

A: 

If you set the userid stored in the session to be the empty string, when you query on it, you will only get users for whom the id is the empty string, which shouldn't be any of them. Therefore, the query is returning an empty set, and your page is (correctly) not displaying a user id.

How are you initially identifying the user? Are you querying against a database when they log in? Are you storing a cookie? Reading Tarot cards? For this to work, at some point, you have to store the correct userid, probably in the session. To do that, you need to first identify who the user is.

Also, if you are using CF6+, you probably do not need the cflock. It is now used to prevent race conditions, as CF is now thread-safe.

Ben Doom
Thanks for you fast reply!I'm using ColdFusion 8, i had better take it off then, thanks. When the user registers their info goes into a MYSQL database, and when they login the identification comes from there - Each user has a different ID and username, then an email and password. I'm not sure what Tarot cards are, or how to use cookies - so I don't think i'm using them - unless the automatically are put in (?). How am I to fix it so the session variable isn't for an empty string?
Bridget
cflock needed for application/server scope only?
Henry
You need a lock anytime you might encounter race conditions. That is, you write a specific value, and if that value is changed, things break, and other threads may change this value. You don't need it for application scope or server scope under normal circumstances, like storing configuration data.
Ben Doom
@bridget, Tarot cards were meant as a joke. They are used to tell the future. Something in your application needs to set the value -- it cannot set itself! How that gets done depends on the architecture of your application, but you probably want to set it when the user logs in.
Ben Doom
haha. ok. Sorry, i'm only 16 - and am only starting with coldfusion. What sort of code should I add into my application - like <cfscript>this.sessionmanagement = true;</cfscript>? Or is that not enough? Thanks for helping me, Im really appreciating it.
Bridget
@Ben - lol. Extra points for accuracy with a "side" of humor ;)
Leigh
@Bridget -- Welcome to the wide community of CF programmers, and I'm glad to see someone new get into programming (however they do it). I would suggest you look at some basic totorials. http://www.easycfm.com/ is a good place for that. http://www.cfnewbie.com looks OK, too. Go through some examples there, and it should help you figure out the basics of application structure.
Ben Doom
I'd also recommend starting with a CF framework. It will help you by making certain things easier for you. CF Wheels is growing in popularity, and for good reason. http://cfwheels.org is definitely worth a look.
Shawn Grigson
A: 

Hi Bridget, Looks like you're just starting with CF, welcome to the community.

My understanding of your code makes the structure look like the following, if I'm understanding you correctly:

<cfset session.idUsers = '' />
<cfquery datasource = "cfgossip" name = "recordset1">
SELECT * FROM USERS WHERE USERS.ID_USERS = <cfqueryparam cfsqltype = "cf_sql_integer" value = "#session.idUsers# />
</cfquery>
<cfoutput>Welcome #recordset1.userID#</cfoutput>

The reason this doesn't work is because your session.idUsers value is blank. Assuming you have a user in your database with an ID_USERS value of 1, you could change the CFSET to look like and it should return results.

Additionally, while it's great to see you using CFQUERYPARAM, I'd recommend including a CFSQLTYPE attribute in the tag whenever possible to provide an added line of defense against injection attacks. You can check out http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags%5Fp-q%5F18.html to see the list of available types.

Kyle Brickman
OK, yer, I am new. I've never made a site to do with Dynamic Development ever before! It's really hard to learn, i've found this site really helpful though - people achually help you here! But i'm not good enough to answers anyone elses questions - so I hope people won't stop helping me thinking im just a 'leacher' if you know what i mean. lol.Anyway, by changing the cfset are you meaning put a value in the ''in =''. Because I want it to get the ID for the user logedin, not always the same value, so i'm unsure of what value to put in. Any ideas? thanks, B.
Bridget
Oh, no worries - the ColdFusion community is always happy to help.Essentially, when your users submit the login form, and in the code that determines that the user has successfully logged on, that's where you're going to want to set the session.idUsers value to be the userID. On that home page, though, you'll want a <cfparam name = "session.idUsers" default = "0" /> to make sure it has something to use in case someone hasn't logged in yet, and then you can check to see if you've got a record count (via <cfif recordset1.recordCount>) to display the welcome message.
Kyle Brickman
A: 

Is there anywhere in your code where you set your session.IDUsers? You initialize it as a blank ''. Coldfusion does not populate it for you. The session scope is a place that will remember things for that user that you put there for a specified period of time inactivity, usually 20 minutes. So hopefully, somewhere before you run your query you have additional logic that fills that in, otherwise you are asking the database for a user named, ''.

This is just a point of style, but the following may work better for you:

<cfset Session.IDUsers =''>

<!--- Do something here to populate Session.IDUsers --->

<!--- Creates a blank query - not necessary, but can reduce errors later --->
<cfset Recordset1 = queryNew("UserID")>

<!--- Populate the query from the database --->
<cfquery name="Recordset1" datasource="cfGossip">
  SELECT *
  FROM   users
  WHERE  users.IDUsers = <cfqueryparam value="#Session.IDUsers#">
</cfquery>

<!--- If the query has data, use it, otherwise show generic message --->
<cfoutput>
<cfif Recordset1.recordcount>
    <p>Welcome #Recordset1.UserID#.</p>
<cfelse>
    <p>Welcome new user!</p>
</cfif>
</cfoutput>

<!--- OR since we used queryNew("userID"), we can simplify without throwing an error. ---->
<cfoutput>
    <p>Welcome <cfif len(Recordset1.userID)>#Recordset1.userID#.<cfelse>new user!</cfif></p>
</cfoutput>

Putting the cfoutput outside the paragraph block will make it easier if you have additional variables to insert into the text. (but will work either way)

Regardless of all that, unless you forgot to share a bit more of the code, I think the issue is that the session.IDUsers is blank and needs to be populated before the query. I hope this helps!

Dan Sorensen