2016-04-27 5 views
0

私は自分のプロジェクト作業のためのeコマースウェブサイトを作成しています。これで私はthis tutorialからのコードを使用して、1つのApplication.cfmページを作成しました:メンバーエリアのみのログインを作成するには

<!--- Create the application ---> 
     <cfapplication name="MyApp" 
      clientmanagement="Yes" 
      sessionmanagement="Yes" 
      sessiontimeout="#CreateTimeSpan(0,0,0,10)#" 
      applicationtimeout="#CreateTimeSpan(0,0,0,10)#" /> 

     <!--- Now define that this user is logged out by default ---> 
     <CFPARAM NAME="session.allowin" DEFAULT="false" /> 

     <!--- Now define this user id to zero by default, this will be used later on to access specific information about this user. ---> 
     <CFPARAM NAME="session.user_id" DEFAULT="0" /> 

     <!--- Now if the variable "session.allowin" does not equal true, send user to the login page ---> 
     <!--- the other thing you must check for is if the page calling this application.cfm is the "login.cfm" page and the "Login_process.cfm" page since the Application.cfm is always called, if this is not checked the application will simply Loop over and over. To check that, you do the following call ---> 

     <cfif session.allowin neq "true"> 
      <cfif ListLast(CGI.SCRIPT_NAME, "/") EQ "loginn.cfm"> 
      <cfelseif ListLast(CGI.SCRIPT_NAME, "/") EQ "login_process.cfm"> 
      <cfelse> 
       <!--- this user is not logged in, alert user and redirect to the login.cfm page ---> 
       <script> 
        alert("You must login to access this area!"); 
        self.location="loginn.cfm"; 
       </script> 
      </cfif> 
     </cfif> 

これはLogin_process.cfmページです:

<!--- Get all records from the database that match this users credentials ---> 
    <cfquery name="qVerify" datasource="cfdb2"> 
     SELECT User_name, User_pass 
     FROM uid_pass 
     WHERE User_name = '#name#' 
    and User_pass='#pass#' 
    </cfquery> 

    <cfif qVerify.RecordCount> 
     <!--- This user has logged in correctly, change the value of the session.allowin value ---> 
      <cfset session.allowin = "True" /> 

     <cfset session.User_name = qVerify.User_name /> 

     <!--- Now welcome user and redirect to "<strong>members_only.cfm</strong>" ---> 
     <script> 
      alert("Welcome user, you have been successfully logged in!"); 
      self.location="index.cfm"; 
     </script> 
    < cfelse> 
     <!--- this user did not log in correctly, alert and redirect to the login page ---> 
     <script> 
      alert("Your credentials could not be verified, please try again!!!"); 
      self.location="Javascript:history.go(-1)"; 
     </script> 
    </cfif> 

とき、私、私はコードで直面しています問題がありますログインするように指示するインデックスページを開きます。ログインしなければ、私は進めることができません。 registration.cfmページを直接開くと、同じことが起こります。ゲストが物にアクセスできるようにコードを構造化するにはどうすればよいですか?「カートに入れる」オプションを使用するとログインする必要があります。

+0

Application.cfmの使用を中止するには、Application.cfcを使用します。 –

答えて

1

ですから、「ホワイトリスト」にログインせずにアクセスすることができる任意のページをする必要がありような何か:。

<cfif session.allowin neq "true"> 
    <!--- check if this is a page that doesn't require authentication ---> 
    <cfset currentScript = ListLast(CGI.SCRIPT_NAME, "/")> 
    <cfif listFindNoCase("login.cfm,registration.cfm,login_process.cfm", currentScript) eq 0> 
     <!--- redirect to login.cfm page ---> 
     <cflocation addtoken="false" href="login.cfm"> 
    </cfif> 
</cfif> 

を私はあなたが本当にあなたがApplication.cfcのを使用する必要があり、のApplication.cfmを使用していることに気付きます。次に、アプリケーションのライフサイクルに入ることができます。セキュリティチェックはonRequestStartメソッドで行うことができ、メソッドなどでセッションを設定することができます。

SQLインジェクション攻撃から自分自身を守るために、常にcfqueryparamをクエリに使用します。ような何か:あなたはプレーンテキストでデータベースにパスワードを保存しているようなあなたのコードから、それは見た目ほど

<cfquery name="qVerify" datasource="cfdb2"> 
    SELECT User_name, User_pass 
    FROM uid_pass 
    WHERE User_name = <cfqueryparam value="#name#" cfsqltype="cf_sql_varchar"> 
     and User_pass = <cfqueryparam value="#pass#" cfsqltype="cf_sql_varchar"> 
</cfquery> 

は、私はまた、あなたがパスワードを保存上に読むことをお勧めしたい - これは悪いです。一方向のパスワード暗号化を使用してみたいと考えています。

関連する問題