title: Coldfusion cookie login
body: <p>I'm new to using cookies to login with coldfusion.</p>
  
  <p>I think I'm relativity close to figuring out how to login with cookies, but still unsure how to have every page refrence the login check properly.
  I want every page in my site to be referenced to the login.</p>
  
  <p>This is the login page:</p>
  
  <pre><code>&lt;cfif IsDefined("cookie.username")&gt;
      &lt;!--- a cookie exist, so let's put in this username automatically into the form ---&gt;
      &lt;cfset username = cookie.username&gt;
  &lt;cfelse&gt;
      &lt;!--- a cookie DOES NOT exist, so let's put a blank value in the username field ---&gt;
      &lt;cfset username = ""&gt;
  &lt;/cfif&gt;
  
  &lt;cfif IsDefined("cookie.password")&gt;
     &lt;!--- a cookie exist, so let's put in this password automatically into the form ---&gt;
      &lt;cfset password = cookie.password&gt;
  &lt;cfelse&gt;
     &lt;!--- a cookie DOES NOT exist, so let's put a blank value in the password field ---&gt;
     &lt;cfset password = ""&gt;
  &lt;/cfif&gt;
  &lt;cfoutput&gt;
  &lt;form action="LoginProcess.cfm" method="post"&gt;
     &lt;table width="500" border="0"&gt;
        &lt;tr&gt;
          &lt;td width="500" colspan="2"&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td width="250"&gt;Username:&lt;/td&gt;
          &lt;td width="250"&gt;&lt;input type="text" name="username" value="#username#"&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td width="250"&gt;Password:&lt;/td&gt;
          &lt;td width="250"&gt;&lt;input type="password" name="password" value="#password#"&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td width="250"&gt;Remember Me&lt;/td&gt;
          &lt;td width="250"&gt;&lt;input type="checkbox" name="RememberMe" value="Yes"
                                     &lt;cfif IsDefined("cookie.username") OR
                                     IsDefined("cookie.password")&gt;CHECKED&lt;/cfif&gt;&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td width="250"&gt;&lt;/td&gt;
          &lt;td width="250"&gt;&lt;input type="submit" name="Process" value="Login"&gt;&lt;/td&gt;
        &lt;/tr&gt;
     &lt;/table&gt;
  &lt;/form&gt;
  &lt;/cfoutput&gt;
  </code></pre>
  
  <p>This is the page the processed the Login process and takes the user to the mainpage of the website.</p>
  
  <pre><code>&lt;cfquery name="qVerify" datasource="SodaWebsite"&gt;
      SELECT        *
      FROM           Admins
      WHERE         Username = '#FORM.username#' AND Password = '#FORM.password#'
  &lt;/cfquery&gt;
  &lt;cfif qVerify.RecordCount&gt;
  
     &lt;!--- this user is good, before actually logging them in, see if their information will be saved for next time ---&gt;
       &lt;cfif IsDefined("RememberMe")&gt;
           &lt;!--- members wants their information remembered, so set the cookies ---&gt;
            &lt;cfcookie name="username" value="#form.username#" expires="NEVER"&gt;
            &lt;cfcookie name="password" value="#form.password#" expires="NEVER"&gt;
      &lt;cfelse&gt;
            &lt;!--- member does NOT want their information remember, EXPIRE their cookies NOW so they are deleted for good! ---&gt;
            &lt;cfcookie name="username" value="#form.username#" expires="NOW"&gt;
            &lt;cfcookie name="password" value="#form.password#" expires="NOW"&gt;
      &lt;/cfif&gt;
  
      &lt;!--- now that you're done with the cookie, follow the REGULAR login procedures as you regularly do ---&gt;
  &lt;/cfif&gt;
  
  
  &lt;cfif IsDefined(cookie.username) and IsDefined(cookie.password)&gt;
  &lt;cfabort&gt;
  &lt;Cfelse&gt;
  &lt;cfinclude template="Index.cfm"&gt;
  &lt;/cfif&gt;
  </code></pre>
  
  <p>For the actual first page I'm only showing up to the header portion, since I believe it goes there.</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;cfif isdefined(Cookie.Username) and isdefined(Cookie.password)&gt;
  &lt;cfabort&gt;
  &lt;cfelse&gt;
  &lt;cfinclude template="LoginProcess.cfm"&gt;
  &lt;/cfif&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  </code></pre>