Page 1 of 1

Database connection problem

Posted: 07 Jun 2010, 23:56
by Sharkguy95
Hi, I am making an MMORPG game and I have had it working for a while, but with no accounts. I just had a breakthough today with getting my config file to connect to my database, and having it attempt to login using the usernames and passwords in the database. The only problem is... its not really working. For the passwords I am using encryption to make sure the passwords are safe, I am wondering if this would effect the login or does it not matter. The name of the table is users, and i have username and password for the information of the users. Here is my dbLogin.as


Code: Select all

function init() {
   dbase = _server.getDatabaseManager();
}

function destroy(){
        //trace("dbLogin extension Closed");
}

function handleRequest(cmd, params, user, fromRoom){
trace("XT Message received: User: " & user & "\n Room: " & fromRoom  & "\n Command: " & cmd); //We don't need any requests
}

function handleInternalEvent(evt) {
   if (evt.name == "loginRequest") {
      trace("recieved event");
      
      var error = "";
       
      var nick = evt["nick"].toLowerCase();
      nick = _server.escapeQuotes(nick);
      var pass = evt["pass"];
      pass = _server.escapeQuotes(pass);
      var chan = evt["chan"];  //Remove this, and the sky will fall on your head
      var qRes = dbase.executeQuery("SELECT * FROM users WHERE password='"+pass+"' AND username = '"+nick+"' LIMIT 1;");
      if (qRes != null) {
         for (var i = 0; i<qRes.size(); i++) {
                  var row  = qRes.get(i);
                  if(row['password'] == pass){
                        error = "";
                  } else {
                        error = "Incorrect Password";
                  }
         }
} else {
error = "Invalid User";
}
var res = new Object();
if(error != ""){
res._cmd = "logKO";
trace("Login of user:"+nick+" failed. Reason:"+error)
} else {
var login = _server.loginUser(nick, pass);
if(login.success){
error = "";
res._cmd = "logOK";
} else {
error = login.error;
}
res.err = error;
}
_server.sendResponse(res,-1,null,chan);
} //END FIRST BLOCK. DO NOT REMOVE
} //DO NOT REMOVE ME EITHER
When I run my game with the custom login turned on, this is the responce I get from the server in the output box.

Code: Select all

[Sending]: <msg t='sys'><body action='verChk' r='0'><ver v='140' /></body></msg>

[Received]: <cross-domain-policy><allow-access-from domain="*" to-ports="9339" /></cross-domain-policy>
[Received]: <msg t="sys"><body action="apiOK" r="0" /></msg>
[Sending]: <msg t='sys'><body action='login' r='0'><login z='Turtles'><nick><![CDATA[Billybob]]></nick><pword><![CDATA[7649d532412dcc7c6d76658f21eb64db]]></pword></login></body></msg>

[Received]: <msg t="xt"><body action="xtRes" r="-1"><dataObj><var n=&apos;_cmd&apos; t=&apos;s&apos;>logKO</var></dataObj></body></msg>
I noticed that it says logKO, which means it isnt working but I dont know why, so what is wrong with this setup? Oh and here is my onExtentionResponce.

Code: Select all

smartfox.onExtensionResponse = function(resObj:Object) { 
        if (resObj._cmd == "logOK") { 
                // Login Success Example Code 
                _global.myName = resObj.name 
                gotoAndStop("chat") 
        } else if (resObj._cmd == "logKO") { 
                // Login failed example code 
            _gloabl.isBusy    = true 
               status_txt.text = "Something has gone wrong. Please refresh the page and try logging in again.";
        } 
}
So what is wrong with the coding? And if you know how, could you post the complete code for the dblogin.as or the complete code for the onExtentionResponce, that would be great thanks!

Posted: 08 Jun 2010, 00:31
by Sharkguy95
The responce above I get from the server when I actually type in the right password for user "Billybob". When I dont type it in correct this is what I get as a responce in the output:

Code: Select all

[Received]: <msg t="xt"><body action="xtRes" r="-1"><dataObj></dataObj></body></msg>
When I look at the server it just says [dbLogin.as]: recieved event

When I type in the REAL password for user billybob it says: [dbLogin.as] Login of user billybob failed. Reason: Incorrect Password

So whats up?

Re: Database connection problem

Posted: 08 Jun 2010, 14:58
by Flappi282
Sharkguy95 wrote: var row = qRes.get(i);
if(row['password'] == pass){
error = "";
I believe this should be

Code: Select all

var row = qRes.get(i);
if([b]row.getItem('password')[/b] == pass){
error = "";

Posted: 08 Jun 2010, 15:32
by Sharkguy95
I just tried that and it gives me lots of errors, so I took out the two b things and it doesnt give me an error. but it doesnt fix the problem, what happens now is it just does nothing for both the correct password and the wrong password, can anyone please help?