12.05.2014, 14:34
Hello everyone!
Im having issues with my MySQL login/registering code. All works fine excpet the moment youre login after registration.
The password is never OK! It always says "wrong password".
Ill paste all the code here:
I hope you guys can help me.
Thanks!
Im having issues with my MySQL login/registering code. All works fine excpet the moment youre login after registration.
The password is never OK! It always says "wrong password".
Ill paste all the code here:
Код:
#include <a_samp> #include <a_mysql> //Nativos native WP_Hash(buffer[], len, const str[]); //MySQL ajustes #define host "localhost" #define user "root" #define db "server" #define pass "" //Dialogs #define dregister 6287 #define dlogin 6288 //Variables globales static mysql, Name[MAX_PLAYERS][24], IP[MAX_PLAYERS][16]; //Enums enum PDATA { ID, Password[129], Admin, VIP, Money, Float:Posx, Float:Posy, Float:Posz } new pInfo[MAX_PLAYERS][PDATA]; main(){} public OnGameModeInit() { mysql_log(LOG_ERROR | LOG_WARNING | LOG_DEBUG); mysql = mysql_connect(host, user, db, pass); if(mysql_errno(mysql) != 0) print("No se ha podido conectar a la base de datos."); } public OnPlayerConnect(playerid) { new query[128]; GetPlayerName(playerid, Name[playerid], 24); GetPlayerIp(playerid, IP[playerid], 16); mysql_format(mysql, query, sizeof(query), "SELECT `Password`, `ID` FROM `players` WHERE `Username` = '%e' LIMIT 1", Name[playerid]); mysql_tquery(mysql, query, "OnAccountCheck", "i", playerid); return 1; } forward OnAccountCheck(playerid); public OnAccountCheck(playerid) { new rows, fields; cache_get_data(rows, fields, mysql); if(rows) { cache_get_field_content(0, "PASS", pInfo[playerid][Password], mysql, 129); pInfo[playerid][ID] = cache_get_field_content_int(0, "ID"); printf("%s", pInfo[playerid][Password]); ShowPlayerDialog(playerid, dlogin, DIALOG_STYLE_INPUT, "Login", "Bienvenido! Porfavor, introduce tu contraseсa para entrar.", "Login", "Salir"); }else { ShowPlayerDialog(playerid, dregister, DIALOG_STYLE_INPUT, "Registro", "Bienvenido! Porfavor, introduce una contraseсa para registrarte.", "Registrar", "Salir"); } return 1; } public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) { switch(dialogid) { case dlogin: { if(!response) Kick(playerid); new hpass[129]; new query[100]; WP_Hash(hpass, 129, inputtext); if(!strcmp(hpass, pInfo[playerid][Password])) { mysql_format(mysql, query, sizeof(query), "SELECT * FROM `players` WHERE `username` = %e LIMIT 1", Name[playerid]); mysql_tquery(mysql, query, "OnAccountLoad", "i", playerid); } else { ShowPlayerDialog(playerid, dlogin, DIALOG_STYLE_INPUT, "Login", "Contraseсa incorrecta!\nIntentalo de nuevo.", "Login", "Salir"); } } case dregister: { if(!response) return Kick(playerid); if(strlen(inputtext) < 6) return ShowPlayerDialog(playerid, dregister, DIALOG_STYLE_INPUT, "Register", "Tu contraseсa es demasiado corta!\nIntroduce una mas larga.", "Registrar", "Salir"); new query[300]; WP_Hash(pInfo[playerid][Password], 129, inputtext); mysql_format(mysql, query, sizeof(query), "INSERT INTO `players` (`Username`, `Password`, `IP`, `Admin`, `VIP`, `Money`, `posx`, `posy`, `posz`) VALUES ('%e', '%s', '%s', 0, 0, 0, 0.0, 0.0, 0.0)", Name[playerid], pInfo[playerid][Password], IP[playerid]); mysql_tquery(mysql, query, "OnAccountRegister", "i", playerid); } } return 1; } forward OnAccountLoad(playerid); forward OnAccountRegister(playerid); public OnAccountLoad(playerid) { pInfo[playerid][Admin] = cache_get_field_content_int(0, "Admin"); pInfo[playerid][VIP] = cache_get_field_content_int(0, "VIP"); pInfo[playerid][Money] = cache_get_field_content_int(0, "Money"); pInfo[playerid][Posx] = cache_get_field_content_float(0, "Posx"); pInfo[playerid][Posy] = cache_get_field_content_float(0, "Posy"); pInfo[playerid][Posz] = cache_get_field_content_float(0, "Posz"); GivePlayerMoney(playerid, pInfo[playerid][Money]); SendClientMessage(playerid, -1, "Datos cargados correctamente"); return 1; } public OnAccountRegister(playerid) { pInfo[playerid][ID] = cache_insert_id(); printf("Nueva cuenta registrada. ID: %d", pInfo[playerid][ID]); return 1; } public OnPlayerDisconnect(playerid, reason) { new query[128], Float:pos[3]; GetPlayerPos(playerid, pos[0], pos[1], pos[2]); mysql_format(mysql, query, sizeof(query), "UPDATE `players` SET `Admin`=%d, `VIP`=%d, `Money`=%d, `posx`=%f, `posy`=%f, `posz`=%f WHERE `ID`=%d", pInfo[playerid][Admin], pInfo[playerid][VIP], pInfo[playerid][Money], pos[0], pos[1], pos[2], pInfo[playerid][ID]); mysql_tquery(mysql, query, "", ""); return 1; } public OnPlayerSpawn(playerid) { SetPlayerPos(playerid, pInfo[playerid][Posx], pInfo[playerid][Posy], pInfo[playerid][Posz]); return 1; }
Thanks!