Help with this mysql code
#1

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:

Код:
#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;
 }
I hope you guys can help me.
Thanks!
Reply
#2

Quote:
Originally Posted by Kratory
Посмотреть сообщение
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:

Код:
#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;
 }
I hope you guys can help me.
Thanks!
Hi there,

Can you start by doing a, "printf("Password after reg: %s", pass); on register/login.

You can then tell by that, it will print what password is entered on login and register, if they are both the same... then hmm, else, it's a broken code somewhere within WP.
Reply
#3

Yup... the registering pass and login one looks the same.
I think the problem could be with the hashing, Doesnt it?


EDIT: YEP! It was a problem with the hashing. (Whirlpool plugin).
I just delete all the hashing code, and it works fine now, but i dont feel confortable by doing just this.
I would love to know WHAT was the problem with the hashing code.
Someone? Some ideas?

Thanks!
Reply
#4

Quote:
Originally Posted by Kratory
Посмотреть сообщение
Yup... the registering pass and login one looks the same.
I think the problem could be with the hashing, Doesnt it?
Then yes, the password somewhat isn't hashing correctly.

When a player connects, not sure how your script works... Does it use a dialog to login,

Can you printf that value, and check what you enter there, matches what you enter at registration time?

If it does, check the Database, and check the password, matches what you enter in the console log.
Reply
#5

Quote:
Originally Posted by UnknownGamer
Посмотреть сообщение
Then yes, the password somewhat isn't hashing correctly.

When a player connects, not sure how your script works... Does it use a dialog to login,

Can you printf that value, and check what you enter there, matches what you enter at registration time?

If it does, check the Database, and check the password, matches what you enter in the console log.
The problem is that mi code is saving the HASHED password (SDNAOСBDAIСUDSBFDIABSOSFN password example) but in the login dialog i is looking for the ORIGINAL password (hihihi123 for example).
I dont know how to do this correctly.
Reply
#6

Quote:
Originally Posted by Kratory
Посмотреть сообщение
The problem is that mi code is saving the HASHED password (SDNAOСBDAIСUDSBFDIABSOSFN password example) but in the login dialog i is looking for the ORIGINAL password (hihihi123 for example).
I dont know how to do this correctly.
Ahh, this makes more sense now, I just scanned through your code.

Is it ONPLAYERCONNECT where it see's the password you enter?
Reply
#7

Yes, it is.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)