2012-01-27 44 views
0

我想使用一個結構來安排三組登錄名和密碼。理想情況下,我希望form.password和form.username通過userThree檢查結構userOne的變量,而不僅僅檢查密碼和用戶名的一個實例。使用結構將變量添加到登錄信息?

任何人都可以指出我需要了解/查看的方向以實現此目的嗎?

預先感謝您。

<cfset userOne = structNew()> 
<cfset userOne.name = "cat"> 
<cfset userOne.password = "meow"> 
<cfset userOne.group = "1"> 

<cfset userTwo = structNew()> 
<cfset userTwo.name = "bird"> 
<cfset userTwo.password = "chirp"> 
<cfset userTwo.group = "2"> 

<cfset userThree = structNew()> 
<cfset userThree.name = "mouse"> 
<cfset userThree.password = "squeak"> 
<cfset userThree.group = "3"> 

<cfset this_password = "squeak"> 
    <cfif form.password neq "squeak" or form.username neq "mouse"> 
    <cflocation url="login.cfm?status=le"> 
     <cfelse> 
     <cflock scope="session" timeout="10"> 
     <cfset session.valid_user = "yes"> 
     </cflock> 
    <cflocation url="index.cfm" addtoken="no"> 
</cfif> 

回答

0
<cfset userOne = structNew()> 
<cfset userOne.name = "cat"> 
<cfset userOne.password = "meow"> 
<cfset userOne.group = "1"> 

<cfset userTwo = structNew()> 
<cfset userTwo.name = "bird"> 
<cfset userTwo.password = "chirp"> 
<cfset userTwo.group = "2"> 

<cfset userThree = structNew()> 
<cfset userThree.name = "mouse"> 
<cfset userThree.password = "squeak"> 
<cfset userThree.group = "3"> 

<cfset this_password = "squeak"> 
    <cfif NOT (authUser(userOne, form.username, form.password) 
     OR authUser(userTwo, form.username, form.password) 
     OR authUser(userThree, form.username, form.password))> 
    <cflocation url="login.cfm?status=le"> 
     <cfelse> 
     <cflock scope="session" timeout="10"> 
     <cfset session.valid_user = "yes"> 
     </cflock> 
    <cflocation url="index.cfm" addtoken="no"> 
</cfif> 


<cffunction name="authUser" access="public" output="false" returntype="boolean"> 
    <cfargument name="user"  type="struct" required="true"> 
    <cfargument name="username" type="string" required="true"> 
    <cfargument name="password" type="string" required="true"> 
    <cfreturn (arguments.user.name EQ arguments.username) AND (arguments.user.password EQ arguments.password)> 
</cffunction> 

這會工作,但它是一個有點混亂。我希望這只是你問題的一個例子,而不是它如何工作。

+0

謝謝您的輸入。它的工作原理是什麼?我最終希望將一個用戶限制在服務器上的一個文件夾中,而另外兩個用戶可以訪問整個站點,但首先希望建立三個用戶及其登錄。 – asha 2012-01-27 10:44:48

+0

我的意思是,在代碼中定義用戶很麻煩。而且,如果真正的情況是首先發生if語句的情況,那麼它通常會更具可讀性。但是你的邏輯是合理的。我希望我的帖子能夠幫助你一點。 – baynezy 2012-02-02 16:41:21

0

如果您可以將3個用戶結構嵌套在用戶結構中,並按名稱鍵入,則可以執行structKeyExists()來查找匹配的用戶,然後只需檢查密碼即可。堆積起來比循環遍佈許多結構更快。

例如。

<cfscript> 
users = { 
    cat = { name: "cat", password: "meow", group: 1 }, 
    bird = { name: "bird", password: "chirp", group: 2 }, 
    mouse = { name: "mouse", password: "squeak", group: 3 } 
}; 

if (
    structKeyExists(form, 'username') 
    && structKeyExists(form, 'password') 
    && len(form.username & form.password) > 0 
    && structKeyExists(users, form.username) 
    && structKeyExists(users[form.username], 'password') 
    && users[form.username].password == form.password 
) { 
    // sign them in 
} 
else { 
    // throw error "invalid username or password" 
} 
</cfscript> 
0

這裏是處理這個法子:

<cfscript> 
users = [{ 
    username = "cat", 
    password = "meow" 
},{ 
    username = "bird", 
    password = "chirp" 
},{ 
    username = "mouse", 
    password = "squeak" 
}]; 

for (user in users) { 
    if (user.username == form.username && user.password == form.password) { 
     // handle success log in 
     // redirect 
    } 
} 

// handle invalid log in 
// redirect 
</cfscript>