27.05.2014, 17:27
(
Last edited by iRaiDeN; 28/05/2014 at 05:27 AM.
)
Hello dear users, today I will explain how to build a Registration & Login simplest way, the database, MySQL database basically it, most programmers think it's difficult, but it is very easy, you will learn to do this in the registration system in the most effective and most have shortened.
Construction
The first thing we should set up variables and defines script.
enum - create the enum.
pName - Actually this variable is intended to "inject" name player this variable.
pPassword - This variable we inject the player sucked password below.
pLevel - Player level, sucked below.
pDeath - Player death, sucked below.
pKills - Player kills, sucked below.
pAdminLevel - Player admin-level, sucked below.
pInfo - This variable is stored in all the details of the enum.
dbHandle - This variable we shall use (compared to the same database connection).
DIALOGֹֹ_REGISTER - define the id of the dialog register.
DIALOG_LOGIN - define the id of the dialog login
DB_SERVER - define the ip of the database (have explain in in the comment[//])
DB_USER - define the username of the database.
DB_PASS - define the pass of the username(root)
DB_DB - define the name of the database.
- Here we compare the variable db Handle, database connection later used.
- Here we inject the name of the player variable pName.
- Here we create the variable for the format.
- Here we select all the values that are in the table accounts, where the username is really, really worth to the player.
- Here we call Public loadPlayerData, that the Public actually sucked all the details of the player (below), with the parameter playerid, where you see true, it's really "Enable cache" and I will use here with the cache to draw (version R7).
- create the public.
- Here we derive the password player.
- Here we derive the number of level of player variable pLevel.
- Here we derive the number of killings of player variable pKills.
- Here we derive the number of deaths of player variable pDeath.
- Here we derive the number of Admin level of player variable pAdminLevel.
- We create a variables.
- Here we are derive the rows && fields from dbHandle.
- Here we check if the user's player is in a table, then it will show him dialog login, but if its user is found it will show a dialog Register.
- if the dialogid equal to defined DIALOG_REGISTER(1)
- if the player click on the button un-response he been kicked.
- If the player left the field blank, it will show him again the dialog.
- Here i create a variable.
- Here we create a new row to a table, and choose what values we want to shoot, we injected in the name of the player && password remover player (inputtext).
- Here we execute the format.
- Here we are send a mesage and spawn the player.
- Here we are check if dialogid equal DIALOG_LOGIN (2)
- if the player click on the button un-resonse the player been kicked.
- If it is left blank it will show him again the dialog.
- If the password is equal to the player tapped his slogan.
- Send a message to the player and spawn the player.
If the password is correct it will show him again the dialog.
- Create a variables.
derive the data of the table (accounts).
- if the player found.
- Here we are update all the values and save them.
--- Done ---
Hope you understand, any help private message successfully.
Construction
The first thing we should set up variables and defines script.
PHP Code:
enum playerEnum {
pName[24],
pPassword[18],
pLevel,
pDeath,
pKills,
pAdminLevel
}
new
pInfo[MAX_PLAYERS][playerEnum] = {-1,...},
dbHandle;
#define DIALOG_REGISTER (1)
#define DIALOG_LOGIN (2)
#define DB_SERVER "127.0.0.1" // put here your ip of the database.
#define DB_USER "root" // the username of the database
#define DB_PASS "" // the pass of the username (root), i do not have a password so i leave this space blank.
#define DB_DB "samp"
pName - Actually this variable is intended to "inject" name player this variable.
pPassword - This variable we inject the player sucked password below.
pLevel - Player level, sucked below.
pDeath - Player death, sucked below.
pKills - Player kills, sucked below.
pAdminLevel - Player admin-level, sucked below.
pInfo - This variable is stored in all the details of the enum.
dbHandle - This variable we shall use (compared to the same database connection).
DIALOGֹֹ_REGISTER - define the id of the dialog register.
DIALOG_LOGIN - define the id of the dialog login
DB_SERVER - define the ip of the database (have explain in in the comment[//])
DB_USER - define the username of the database.
DB_PASS - define the pass of the username(root)
DB_DB - define the name of the database.
PHP Code:
public OnGameModeInit()
{
dbHandle = mysql_connect(DB_SERVER,DB_USER,DB_DB,DB_PASS);
return 1;
}
PHP Code:
dbHandle = mysql_connect(DB_SERVER,DB_USER,DB_DB,DB_PASS);
PHP Code:
public OnPlayerConnect(playerid)
{
GetPlayerName(playerid,pInfo[playerid],24);
static
gQuery[128];
format(gQuery,128,"SELECT * FROM accounts WHERE username='%s'",pInfo[playerid][pName]);
mysql_function_query(dbHandle,gQuery,true,"loadPlayerData","i",playerid);
return 1;
}
PHP Code:
GetPlayerName(playerid,pInfo[playerid],24);
PHP Code:
static
gQuery[128];
PHP Code:
format(gQuery,128,"SELECT * FROM accounts WHERE username='%s'",pInfo[playerid][pName]);
PHP Code:
mysql_function_query(dbHandle,gQuery,true,"loadPlayerData","i",playerid);
PHP Code:
forward loadPlayerData(playerid);
public loadPlayerData(playerid)
{
cache_get_field_content(0,"password",pInfo[playerid][pPassword],dbHandle,18);
pInfo[playerid][pLevel] = cache_get_field_content_int(0,"level",dbHandle);
pInfo[playerid][pKills] = cache_get_field_content_int(0,"kills",dbHandle);
pInfo[playerid][pDeath] = cache_get_field_content_int(0,"death",dbHandle);
pInfo[playerid][pAdminLevel] = cache_get_field_content_int(0,"adminlevel",dbHandle);
new rows,fields;
cache_get_data(rows,fields,dbHandle);
if(rows != 0)
{
ShowPlayerDialog(playerid,DIALOG_LOGIN,DIALOG_STYLE_PASSWORD,"Login","Please enter your password to login","Login","Logout");
} else {
ShowPlayerDialog(playerid,DIALOG_REGISTER,DIALOG_STYLE_PASSWORD,"Register","Please enter password to register","Reg","Logout");
}
}
PHP Code:
forward loadPlayerData(playerid);
PHP Code:
cache_get_field_content(0,"password",pInfo[playerid][pPassword],dbHandle,18);
PHP Code:
pInfo[playerid][pLevel] = cache_get_field_content_int(0,"level",dbHandle);
PHP Code:
pInfo[playerid][pKills] = cache_get_field_content_int(0,"kills",dbHandle);
PHP Code:
pInfo[playerid][pDeath] = cache_get_field_content_int(0,"death",dbHandle);
PHP Code:
pInfo[playerid][pAdminLevel] = cache_get_field_content_int(0,"adminlevel",dbHandle);
PHP Code:
new rows,fields;
PHP Code:
cache_get_data(rows,fields,dbHandle);
PHP Code:
if(rows != 0)
PHP Code:
public OnDialogResponse(playerid,dialogid,response,listitem,inputtext[])
{
if(dialogid == DIALOG_REGISTER)
{
if(!response) return Kick(playerid);
if(!strlen(inputtext)) return ShowPlayerDialog(playerid,DIALOG_REGISTER,DIALOG_STYLE_PASSWORD,"Register","Please enter password to register!","Reg","Logout");
static
gQurey[128];
format(gQuery,128,"INSERT INTO accounts(username,password,kills,death,level,adminlevel) VALUES('%s','%s','0','0','1','0')",pInfo[playerid][pName],inputtext);
mysql_function_query(dbHandle,gQuery,false,"","");
SendClientMessage(playerid,-1,"you registerd successuflly!");
SpawnPlayer(playerid);
return 1;
}
if(dialogid == DIALOG_LOGIN)
{
if(!response) return Kick(playerid);
if(!strlen(inputtext)) return ShowPlayerDialog(playerid,DIALOG_LOGIN,DIALOG_STYLE_PASSWORD,"Login","Please enter your password to login!","Login","Logout");
if(!strcmp(inputtext,pInfo[playerid][pPassword],true))
{
SendClientMessage(playerid,-1,"You logged successuflly!");
SpawnPlayer(playerid);
} else {
ShowPlayerDialog(playerid,DIALOG_LOGIN,DIALOG_STYLE_PASSWORD,"Login","Invalid password!","Login","Logout");
}
}
}
PHP Code:
if(dialogid == DIALOG_REGISTER)
PHP Code:
if(!response) return Kick(playerid);
PHP Code:
if(!strlen(inputtext)) return ShowPlayerDialog(playerid,DIALOG_REGISTER,DIALOG_STYLE_PASSWORD,"Register","Please enter password to register!","Reg","Logout");
PHP Code:
static
gQurey[128];
PHP Code:
format(gQuery,128,"INSERT INTO accounts(username,password,kills,death,level,adminlevel) VALUES('%s','%s','0','0','1','0')",pInfo[playerid][pName],inputtext);
PHP Code:
mysql_function_query(dbHandle,gQuery,false,"","");
PHP Code:
SendClientMessage(playerid,-1,"you registerd successuflly!");
SpawnPlayer(playerid);
PHP Code:
if(dialogid == DIALOG_LOGIN)
PHP Code:
if(!response) return Kick(playerid);
PHP Code:
if(!strlen(inputtext)) return ShowPlayerDialog(playerid,DIALOG_LOGIN,DIALOG_STYLE_PASSWORD,"Login","Please enter your password to login!","Login","Logout");
PHP Code:
if(!strcmp(inputtext,pInfo[playerid][pPassword],true))
PHP Code:
SendClientMessage(playerid,-1,"You logged successuflly!");
SpawnPlayer(playerid);
PHP Code:
} else {
ShowPlayerDialog(playerid,DIALOG_LOGIN,DIALOG_STYLE_PASSWORD,"Login","Invalid password!","Login","Logout");
PHP Code:
public OnPlayerDisconnect(playerid,reason)
{
new rows,fields;
cache_get_data(rows,fields,dbHandle);
if(rows != 0)
{
static
gQuery[128];
format(gQuery,128,"UPDATE accounts SET kills='%d',death='%d',level='%d',adminlevel='%d'",pInfo[playerid][pKills],pInfo[playerid][pDeath],pInfo[playerid][pLevel],pInfo[playerid][pAdminLevel]);
mysql_function_query(dbHandle,gQuery,false,"","");
}
}
PHP Code:
new rows,fields;
PHP Code:
cache_get_data(rows,fields,dbHandle);
PHP Code:
if(rows != 0)
PHP Code:
static
gQuery[128];
format(gQuery,128,"UPDATE accounts SET kills='%d',death='%d',level='%d',adminlevel='%d'",pInfo[playerid][pKills],pInfo[playerid][pDeath],pInfo[playerid][pLevel],pInfo[playerid][pAdminLevel]);
mysql_function_query(dbHandle,gQuery,false,"","");
--- Done ---
Hope you understand, any help private message successfully.