tags:

views:

68

answers:

2

Here my code to create a session variable:

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

I need to put a value in = "" but I want the value to change to be the users ID depending on which user is logged in. So I can't just put any number etc. in there. What do I have to do?

+1  A: 

Basically, you're going to do this when you log your user in. A sign-in routine might look like the following:

<cfquery datasource="cfgossip" name="signin">
  SELECT 
    ID_USERS 
  FROM 
    USERS 
  WHERE 
    UN = <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.un#" /> 
    AND PW = <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.pw#" />
</cfquery>
<cfif signin.recordCount>
  <cfset session.idUsers = signin.id_users />
</cfif>

Until they've logged in, you can't know what the value you need is. However, once you've determined that they are who they say they are and you're going to let them in, you can then set the session variable.

Kyle Brickman
OK, I put in my values, but I'm not sure what to put instead of 'un', and the browser errors with 'Element UN is undefined in FORM. '. IS the rest of my code correct do you think? <cfquery datasource = "cfgossip" name = "users">SELECT IDUsers FROM USERS WHERE UN = <cfqueryparam cfsqltype = "cf_sql_varchar" value = "#form.un#" /> AND PW = <cfqueryparam cfsqltype = "cf_sql_varchar" value = "#form.pw#" /></cfquery><cfif signin.recordCount><cfset session.IDUsers = signin.IDUsers /></cfif>Thanks for helping my by the way - it's really helping me out.
Bridget
Are you posting a form to the page this code appears upon? If this is on your signin page, you'd replace form.un with whatever you've put in the NAME attribute of your INPUT field for the user name, and likewise for the password. Or am I missing something?Happy to help. By the way, just for some encouragement, it does get easier - personally, what helped me a lot was hitting the LiveDocs (http://livedocs.adobe.com/coldfusion/8/) for virtually every tag I used to see what I could do with it - they often provide sample code there as well.
Kyle Brickman
ok, cool I start using the into LiveDocs - any help is good. Did you learn ColdFusion by just doing what i'm going to? It's really hard, theres all this jargon which i don't understand.lol. I only have this one site i really want to make before christmas - but i don't acuhally think that's going to happen. lol. Yer, I do have my session variable code on the pages i'm working on... on the login i have the create session etc. am I not meant to? I have on my application.cfc this.sessionmanagement = true; - is all the code meant to be there...?? o no...opps.lol
Bridget
Well, I had a head start, since I've got a bachelors in CS, but it took a lot of work for me to get into thinking for web development, but now I've been doing it a year and a half professionally, and I'm enjoying it!Your application.cfc is only for stuff that's going to be applied across your CF application (like session management). You'd have the code I mentioned on a seperate login page that you'd post an HTML form to (with INPUT elements for UN, PW, etc.).
Kyle Brickman
ok. Well i've never known about any of the seperate pages before - i'll try use the 'LiveDocs' to get a start so i don't bor you with simple questions. Is the code you gave me (from the answer we writing from above) the correct code for my to add to the seperate login page i'm going to make? Then do i add anycode on the application or login page about linking to the seperate login page - or does all the code about linking come from the seperate login page? ... ok, this looks really complicated - but i'll give it a try! you must make alot from making these sort of sites - I bet there isn't ....
Bridget
...much competition. I think i'm going to learn it, but not do it professionally. It seems really hard, and stressful if it doesn't work properly!
Bridget
Well, doing difficult things is frequently rewarding, so it's worth it for me. Basically, what I find helps is to break each part of your site up into tasks, i.e. if you were making a blog, you'd have a .cfm page to display excerpts from all the blog posts, a .cfm page to display a full blog post, a .cfm to display comments, a .cfm to sign in to post comments, a .cfm for special (signed-in) users to write blog posts, etc. Once you get the basics down, moving on to more advanced functionality (templates, frameworks, and other things you don't need to worry about yet) will be much easier.
Kyle Brickman
ok cool. I have kind of done that for my site. I started with the login /register /logout. Then made the search etc. and now are creating the add comment. I also want to create a page for users to view 'their added comments' and a 'my Favourite comment'page. How am I to do this? Is this also using Session Variables? After that the only thing I have left is to create a system like on this site - allowing users to comment on other comments - but that seems really hard - I have tried learning it for about a week flat! and still don't know anything! Do I have to tie the comments ID's or something?
Bridget
Well, on this site, there are answers, and the comments on the answer, but realistically, they're pretty much the same. If you have a 'parent id' on the comment in the DB, you can set the replies to have the parent ID of the original comment, and then you can order them by the comment ID, which should get you sequential order.Regarding the users' comments and favorites, those will need to be stored in the DB. The session defaults to 30 minutes of inactivity, after that, they will lose their session. 'Permanent' storage should be done in the DB, or in files for things like images.
Kyle Brickman
Great! thats really helpful. I'm just woundering, the reason I am learning session.v. is so that I can put the Users ID from the user with the comment they add in the comments table to filter the 'users' comments' and 'favorites' pages for each user. Is that the way to do it, using session variables? or is there some simple INSERT INTO comments (IDUser) SELECT IDUsers FROM users - or something like that? (obvisualy the code will be a more compicated...nothings that simple with these progams lol). What do you think? Do I have to use session.v. or is there a better option?
Bridget
If you've stored a user ID in the session after a user has logged in, you can then use it to set the IDUSER column in your comments table. The session is the best way to pass data from page to page in a manner in which the user cannot modify it, which is obviously good when it comes to establishing someone's identity.
Kyle Brickman
o great. Thanks. Just another short thing...is this code enough to make a session variable on the login page:<cflock timeout=20 scope="Session" type="Exclusive"> <cfset Session.IDUsers = ''></cflock>Or do I need to make the PW, UN page?
Bridget
Well, you're going to need a signin page regardless, right? After the user submits the form on that page, wherever the form submits to should set the session ID.
Kyle Brickman
A: 

Ok I think you need a basic breakdown of how this should work. Step 1: User goes to a section of your website/app that needs login. You check if the session.userid is set to a valid value. If not goto login screen.

Step 2: Present user with a login form.

<form action="checklogin.cfm"> <input type="text" name="username" value=""> <input type="password" name="pass" value=""> <input type="submit" value="login"> </form>

Step 3: On clicking login the form is submitted to an action page which checks if the credentials supplied match a valid user.

<cfquery datasource = "myDB" name = "getUsers"> SELECT userID FROM USERS WHERE username = <cfqueryparam cfsqltype = "cf_sql_varchar" value = "#form.username#" /> AND password = <cfqueryparam cfsqltype = "cf_sql_varchar" value = "#form.pass#" /> </cfquery>

Step 4: If valid user goto the logged in area else return to login screen

<cfif getUsers.recordCount GT 0> <cfset session.IDUsers = getUsers.userID /> <cflocation url="home page for logged in users"> <cfelse> <cflocation url="return to login form and display invalid login message"> </cfif>

This is a very basic login form but it should get you started.

SNeiland