开发者

Value for my session variable?

Here my code to create a session variable:

<cflock timeout="999" scope="Session" type="Exclusive">
  <cfset Sessio开发者_运维问答n.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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜