MySQL Register & Login [wip]
#1

Ok. Here is my tutorial:

Firstly we must define our mysql configuration & include:
pawn Code:
#include <a_samp>
#include <a_mysql>

#define MYSQL_HOST "localhost"
#define MYSQL_USER "root"
#define MYSQL_PASS ""
#define MYSQL_DB "DBNAME" // set this to your dbname
We must next physically connect it:
pawn Code:
public OnGameModeInit()
{
     mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB);
     mysql_debug(0);
}
After configing it, we must make the register system:
pawn Code:
public OnPlayerConnect(playerid)
{
    new query[126];
    format(query, sizeof(query), "SELECT * FROM accounts WHERE username = '%s'", pName(playerid)); // selects our name
    mysql_query(query); // will run the process
    mysql_store_result(); // stores the result
   
    if(mysql_num_rows() == 1) // if the username is on the column
    {
        SendClientMessage(playerid, -1, "Registered user");
        ShowPlayerDialog(playerid, 0, DIALOG_STYLE_PASSWORD, "Login", "Hello, please input your password.", "Login", "Exit");
    }
    else // if it is not on the database
    {
        ShowPlayerDialog(playerid, 1, DIALOG_STYLE_PASSWORD, "Register", "Hello, please register with a pass below.", "Register", "Exit");
    }
    return 1;
}

We must now register it:
pawn Code:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    switch(dialogid)
    {
        case DIALOG_LOGIN:
        {
            new dialog[500];
            if(!response) return Kick(playerid); // if escape is hit
            if(response) // if they hit enter/login
            {
                new query[100], pname[24];
                GetPlayerName(playerid, pname, 24);
                format(query, sizeof(query), "SELECT `username` FROM accounts WHERE username = '%s' AND password = sha1('%s')", pname, inputtext); // getting some stats, selecting username and hashed password
                mysql_query(query); // calls the function
                mysql_store_result(); // stores the function
                new nrows = mysql_num_rows(); // defining our rows so it is smaller and easier to use
                if(nrows == 1) LoginProcess(playerid); // if the user is there, the login shit will execute
                if(!nrows) // if no rows are there
                {
                    format(dialog, sizeof(dialog), "Hello, please input your password."); // resubmitting the dialog
                    ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", dialog, "Login", "Exit");
                }
                mysql_free_result();
            }
        }
        case DIALOG_REG:
        {
            new string[500];
            if(response)
            {
                if(!strlen(inputtext) || strlen(inputtext) > 50) // if the inputtext is over 50
                {
                    format(string, sizeof(string), "Hello, %s, this name is unregistered. You may register now.", pName(playerid));
                    ShowPlayerDialog(playerid, DIALOG_REG, DIALOG_STYLE_PASSWORD, "Register", string, "Register", "Exit");
                }
                else if(strlen(inputtext) > 0 && strlen(inputtext) < 50) // if it is below 50 and above 0
                {
                    RegisterProcess(playerid, inputtext); // executing registration information
                }
            }
        }
    }
    return 1;
}
Reply
#2

Better use R7 plugin for speed of mysql
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)