SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 14/07/2013, 12:36 AM   #1
Mitchy
Banned
 
Join Date: Feb 2013
Posts: 141
Reputation: -15
Default MySQL Register & Login [wip]

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;
}
Mitchy is offline   Reply With Quote
Old 14/07/2013, 12:48 PM   #2
JetForever
Little Clucker
 
Join Date: Jul 2013
Location: Lithuania
Posts: 3
Reputation: 0
Default Re: MySQL Register & Login [wip]

Better use R7 plugin for speed of mysql
JetForever is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tutorial] Mysql Register/Login system! FireCat Tutorials 55 21/04/2016 06:14 PM
[Tutorial] Login Register System - MySQL Omirrow Tutorials 4 28/03/2013 10:33 PM
mysql r7 register/login problem newbienoob Scripting Help 18 23/03/2013 06:28 AM
Mysql Register Login BaranHyrn Scripting Help 0 21/12/2011 06:33 PM
MySQL Login And Register quite_scream Help Archive 2 16/02/2010 02:13 PM


All times are GMT. The time now is 09:17 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.