2

I am making a user signup app for an ecomerce site, but I have run into a very odd problem. When I run this code:

<cfif isValid("email", form.email)>
    <cfquery name="check_user" datasource="#request.dsn#">
        SELECT var_username, var_password
        FROM tbl_users
        WHERE var_username = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#FORM.EMAIL#"> 
        AND var_password = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#FORM.PASSWORD#"> 
    </cfquery>
    <cfif check_user.recordcount LT 1>
        <cfquery datasource="#request.dsn#" name="insertuser">
            INSERT INTO tbl_users (var_username, var_password) 
            VALUES
            (<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#FORM.EMAIL#">,
            <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#FORM.PASSWORD#">)
        </cfquery>


        <cflogin idletimeout="1800">
        <cfloginuser 
                name = "#FORM.email#"
                password ="#FORM.password#"
                roles = "0">

        </cflogin>
        <cflocation addtoken="No" url="#request.secure_url#checkout.cfm">
    <cfelse>
        <cfset client.error_message = "Your Email Address is already registered.">
        <cflocation addtoken="No" url="#request.site_url#New-Account.html">
    </cfif>

<cfelse>
    <cfset client.error_message = "Your Email Address is not Valid.">
    <cflocation addtoken="No" url="#request.site_url#New-Account.html">

</cfif>

What this code is supposed to do, is check to see if the email is already in the database, if its not, it then adds the email/password into the database then logs them in and moves them to the secure checkout site. What the code is actually doing, is quite different though. It is checking to see if they are in the database, and then adding them, however, after it adds them to the database it skips the login and the cflocation and goes to the cfelse, going back to the original page "New-Account" and displaying the client.error_message.

How is it possible for the code to run the cfif check_user.recordcount and then part way through it skip to the cfelse?

Things I have done to try and narrow down whats happening-

It is getting pass the check_user statement and adding a user, because I have direct access to the database and after trying to signup a user, I delete it and try again (and notice that when I try to create it, it does create it), so I know its getting past the insertuser cfquery. I have changed the cflocation to redirect to google, but it doesnt, and I also have it show on the page whether or not I am logged in, so I know that it is skipping to the cfelse before the cflogin.

  • are you getting redirected to checkout.cfm at all? – DG3 Mar 08 '12 at 13:47
  • No, for this cfCase, it won't redirect me to checkout, however in other cfCase's that I have on the page (IE checkout as guest, and signin and checkout) both of those work smoothly and pass you on to checkout.cfm. – Brandon Allison Mar 08 '12 at 15:02

1 Answers1

3

I would suggest commenting out all of your cflocations temporarily and seeing if your behaviour is the same. It could be that your login process doesn't finish before your cflocation starts.

Or, it could be that your page submits to itself correctly the first time, and then the second time hits the else clause.

There is nothing in your code that is obviously wrong to me.

Evik James
  • 10,335
  • 18
  • 71
  • 122
  • this sounds like the most likely culprit to me as well. The code looks correct to me. – Matt Busche Mar 08 '12 at 01:17
  • Agreed. I don't think it's this code that is the problem, it's how things are being routed to and from this page (and the "New Account" page) that is likely to be the culprit. Have you checked things like any unexpected HTTP redirects going on? – Adam Cameron Mar 08 '12 at 07:08
  • I took out the CFLocations but left the cfLogin, and it still wouldn't login, but would add the email to the database. Then I took out the CfLogin and left the CFLocation, it would still add the email but wouldn't change the cfLocation. Also, that code is wrapped in a CFCase that won't run unless a hidden form value is submitted, so I don't think that it's possible for the code to be hit a second time. – Brandon Allison Mar 08 '12 at 14:59
  • Ok, thanks to Evik James, I was able to rule out both the CFLocation, and the CFLogin. After commenting out the CFQuery "insertuser", the page now successfully logs you in and redirects you. So, something about the cfquery must be causing a hang up while it adds the user to the database, I just need to figure out what. – Brandon Allison Mar 08 '12 at 15:07
  • 1
    @BrandonAllison, I am glad to help. If my answer helped you and leads to your answer, don't hesitate to mark mine as the right answer. Also, for a first time question poster, you did a fantastic job with your question. I look forward to helping you and being helped by you in the future! – Evik James Mar 08 '12 at 15:13
  • @EvikJames Yea, sorry about that, I forgot to mark it as answered. I have been a long time lurker here, this was just the first problem that really made no sense to me, so I had to post it. And to be honest, it still doesn't make a lot of sense to me. You wouldn't happen to have any insight on why a CFQuery messing up would cause a cfif statement to go skip down to the CFElse part way through would you? One would think that if a CFIF's requirements are met, it would stay in the CFIF and not go to the CFElse unless the CFIF's requirements weren't met. – Brandon Allison Mar 08 '12 at 15:23
  • 2
    Generally, weird behavior is just a misunderstanding. I'm sure it's doing exactly what you are telling it to do. You've just got some buggy logic somewhere. Take it apart. Put it back together again. You'll find the problem and fix it -- eventually. Just keep asking questions. – Evik James Mar 08 '12 at 15:28
  • I agree with Evik. The page is likely doing exactly what it's supposed to do as it's written. Use cfdump, javascript alert()s or console.log in various places and see what is actually getting called, set and run. And in what order. That should help you figure out where the logic is getting wonky. – Shawn Apr 24 '14 at 16:54