Automatic Login?
#1

So I have finally finished some code, but I want to have the system auto-login after registering. How could I do that?

pawn Код:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    switch(dialogid)
    {
        case 1:
        {
            if(!response)
            {
                SendClientMessage(playerid, COLOR_RED, "You selected exit, therefore you were kicked.");
                Kick(playerid);
            }
            GetFromAccount(PlayerStatistics[playerid][pDatabaseID], "Password", PlayerStatistics[playerid][pPassword]);
            if(strcmp(inputtext, PlayerStatistics[playerid][pPassword], true)== 0)
            {
                new string[128], name[MAX_PLAYER_NAME];
                GetPlayerName(playerid, name, sizeof(name));
                format(string, sizeof(string), "You have successfully logged in, %s. Welcome back to %s!", name, SNAME);
                SendClientMessage(playerid, COLOR_WHITE, string);
                SetPlayerColor(playerid, COLOR_WHITE);
                name = GetName(playerid);
                format(string, sizeof(string), "%s (%d)\n Score: %i", name, playerid, GetPlayerScore(playerid));
                pNameTag[playerid] = Create3DTextLabel(string, COLOR_WHITE, 30.0, 40.0, 50.0, 15.0, -1, 1);
                Attach3DTextLabelToPlayer(pNameTag[playerid], playerid, 0.0, 0.0, 0.2);
                PlayerStatistics[playerid][pAuth] = 1;
                LoadAccountVariables(playerid);
            }
            else
            {
                SendClientMessage(playerid, COLOR_WHITE, "Incorrect password. Please try again!");
                ShowPlayerDialog(playerid, 1, DIALOG_STYLE_INPUT, "Login / Authentication Process", "Hello there!\n\nPlease enter your password to authenticate.", "Login", "Cancel");
            }
        }
        case 2:
        {
            if(strlen(inputtext) >= 30)
            {
                ShowPlayerDialog(playerid, 2, DIALOG_STYLE_INPUT, "Registration Process", "Hello there!\n\nPlease enter a password to register.", "Authenticate", "Cancel");
                SendClientMessage(playerid, COLOR_WHITE, "[ERROR]: Your password must be less than 30 characters in length.");
            }
            else
            {
                if(strlen(inputtext) >= 4)
                {
                    new EscapedPassword[30], Query[128], name[MAX_PLAYER_NAME], EscapedName[MAX_PLAYER_NAME];
                    GetPlayerName(playerid, name, sizeof(name));
                    mysql_real_escape_string(name, EscapedName);
                    mysql_real_escape_string(inputtext, EscapedPassword);
                    #pragma unused name
                    format( Query, sizeof( Query ), "INSERT INTO `Accounts` (Username, Password) VALUES('%s', '%s')", EscapedName, EscapedPassword );
                    mysql_query( Query );
                    GivePlayerMoney(playerid, NOOBMONEY);
                    InitPlayerConnection(playerid);
                }
                else
                {
                    ShowPlayerDialog(playerid, 2, DIALOG_STYLE_INPUT, "Registration Process", "Hello there!\n\nPlease enter a password to register.", "Authenticate", "Cancel");
                    SendClientMessage(playerid, COLOR_WHITE, "[ERROR]: Your password must be at least 4 characters in length.");
                }
            }
        }
    }
}

// ------------------ [MySQL Accounting System] ------------------ //

stock ResetPlayerVariables(playerid)
{
    PlayerStatistics[playerid][pAuth] = 0;
    PlayerStatistics[playerid][pAdminLevel] = 0;
    PlayerStatistics[playerid][pDatabaseID] = 0;
    PlayerStatistics[playerid][pMoney] = 0;
    PlayerStatistics[playerid][pScore] = 0;
    return 1;
}

stock InitPlayerConnection(playerid)
{
    ResetPlayerVariables(playerid);
    new Query[128], name[MAX_PLAYER_NAME], EscapedName[MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, sizeof(name));
    mysql_real_escape_string(name, EscapedName);
    #pragma unused name
    format(Query, sizeof(Query), "SELECT `UserID` FROM `Accounts` WHERE `Username` = '%s'", EscapedName);
    mysql_query(Query);
    mysql_store_result();
    if(mysql_num_rows()>= 1)
    {
        if(mysql_num_rows()>= 2)
        {
            mysql_free_result();
            SendClientMessage(playerid, COLOR_WHITE, "There seem to be duplicate accounts under your name. Please contact a level 4 admin.");
            Kick(playerid);
        }
        else
        {
            PlayerStatistics[playerid][pDatabaseID] = mysql_fetch_int();
            mysql_free_result();
            ShowPlayerDialog(playerid, 1, DIALOG_STYLE_INPUT, "Login / Authentication Process", "Hello there!\n\nPlease enter your password to authenticate.", "Login", "Cancel");
            SendClientMessage(playerid, COLOR_WHITE, "Your account exists. Please enter your password to proceed.");
        }
    }
    else
    {
        mysql_free_result();
        SendClientMessage(playerid, COLOR_WHITE, "Your account does not yet exist. Enter a password to register one!");
        ShowPlayerDialog(playerid, 2, DIALOG_STYLE_INPUT, "Registration Process", "Hello there!\n\nPlease enter a password to register.", "Register", "Cancel");
    }
}

stock SavePlayerAccount(playerid)
{
    new string[512];
    if(PlayerStatistics[playerid][pAuth] == 1)
    {
        PlayerStatistics[ playerid ][ pScore ] = GetPlayerScore(playerid);
        PlayerStatistics[ playerid ][ pMoney ] = GetPlayerMoney(playerid);
        format(string, sizeof(string), "UPDATE `Accounts` SET `Password` = %s, `AdminLevel` = %d, `Money` = %d, `Score` = %d WHERE `UserID` = %d", PlayerStatistics[playerid][pPassword], PlayerStatistics[playerid][pAdminLevel], PlayerStatistics[playerid][pMoney], PlayerStatistics[playerid][pScore], PlayerStatistics[playerid][pDatabaseID]);
        mysql_query(string);
    }
    return 1;
}

stock LoadAccountVariables(playerid)
{
    if(IsPlayerConnected(playerid))
    {
        new DataString[128], Query[128];
        format(Query, sizeof(Query), "SELECT * FROM `Accounts` WHERE `UserID` = '%d'", PlayerStatistics[playerid][pDatabaseID]);
        mysql_query(Query);
        mysql_store_result();
        mysql_fetch_field("AdminLevel", DataString);
        mysql_fetch_field("Money", DataString);
        mysql_fetch_field("Score", DataString);
        PlayerStatistics[playerid][pAdminLevel] = strval(DataString);
        PlayerStatistics[playerid][pMoney] = strval(DataString);
        PlayerStatistics[playerid][pScore] = strval(DataString);
        GivePlayerMoney(playerid, PlayerStatistics[playerid][pMoney]);
        SetPlayerScore(playerid, PlayerStatistics[playerid][pScore]);
        mysql_free_result();
    }
    else
    {
        printf("[MySQL ERROR] LoadAccountVariables() was called, but to a non-connected ID.", playerid);
    }
}

stock GetFromAccount(dbid, obtaining[], holdingvar[])
{
    new Query[128];
    format(Query, sizeof(Query), "SELECT `%s` FROM `Accounts` WHERE `UserID` = '%d'", obtaining, dbid);
    mysql_query(Query);
    mysql_store_result();
    if(mysql_fetch_row(holdingvar)== 1)
    {
        mysql_free_result();
    }
    return 1;
}
Reply
#2

When the player registers, save their IP.

new pIP[16];
GetPlayerIP(playerid, pIp, sizeof(pIP));

When the player connects, compare the IP in the user file with the IP the user logged in with. Once the user is logged in, save the IP to the user file again (incase they logged in manually with a different IP).
Reply
#3

I know what you mean. It's a tad bit more than what you mentioned, but I could probably do it. However, how do I auto-login players when they register?
Reply
#4

Whatever variable you alter when the player logs in, simply change the same variable when they register.

Example:

On log, PlayerLoggedIn[playerid] is changed to 1, meaning they can spawn.

When they register, PlayerLoggedIn[playerid] is changed to 1, meaning they are logged in and can spawn.

Hopefully this clears things up.
Reply
#5

Quote:
Originally Posted by Conroy
Посмотреть сообщение
Whatever variable you alter when the player logs in, simply change the same variable when they register.

Example:

On log, PlayerLoggedIn[playerid] is changed to 1, meaning they can spawn.

When they register, PlayerLoggedIn[playerid] is changed to 1, meaning they are logged in and can spawn.

Hopefully this clears things up.
Okay, so I added "PlayerStatistics[playerid][pAuth] == 1", which was my variable for that, so it works now. Thank you!
Reply
#6

No problem. Here to help.

-Conroy
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)