26.08.2016, 17:42
Hi, How do I withdraw IP address of the player's names ?
MySQL Version: R39-5
MySQL Version: R39-5
SELECT `IP` FROM `users` WHERE `Name` = '%e'
SELECT `Name` FROM `users` WHERE `IP` = '%s'
If you want to get a player's IP:
Код:
SELECT `IP` FROM `users` WHERE `Name` = '%e' If you want to get all the names that share a certain IP: Код:
SELECT `Name` FROM `users` WHERE `IP` = '%s' |
CMD:aka(playerid,params[]) { new isim[24]; if(sscanf(params, "s[24]", isim)) return SendClientMessage(playerid, -1, "{D324FF}Kullanım: {FFFFFF}/Aka (IP)"); new query[128]; mysql_format(g_SQL, query, sizeof(query), "SELECT `Isim` FROM `oyuncudata` WHERE `IPAdress` = '%s'", isim); mysql_tquery(g_SQL, query, "AkaCek", "ds", playerid,isim); return 1; } function:AkaCek(playerid, ip_adress[]) { new rows, fields; cache_get_data(rows, fields); if(rows) { for(new i = 0; i < rows; ++i) { new isim[24]; cache_get_field_content(i, "Isim", isim); printf("%s\n",isim); } } else { SendClientMessage(playerid, ERROR_COLOR_HD, "yok."); } return 1; }
If you want to get a player's IP:
Код:
SELECT `IP` FROM `users` WHERE `Name` = '%e' If you want to get all the names that share a certain IP: Код:
SELECT `Name` FROM `users` WHERE `IP` = '%s' |
static GetIPVal(const ip[])
{
new len = strlen(ip);
if (!(len > 0 && len < 17))
return 0;
new count;
for (new i; i < len; i++)
{
if (ip[i] == '.')
count++;
}
if (!(count == 3))
return 0;
new address = strval(ip) << 24;
count = strfind(ip, ".", false, 0) + 1;
address += strval(ip[count]) << 16;
count = strfind(ip, ".", false, count) + 1;
address += strval(ip[count]) << 8;
count = strfind(ip, ".", false, count) + 1;
address += strval(ip[count]);
return address;
}
stock IpMatch(const ip1[], const ip2[], rangetype = 26)
{
new ip = GetIPVal(ip1);
new subnet = GetIPVal(ip2);
new mask = -1 << (32 - rangetype);
subnet &= mask;
return bool:((ip & mask) == subnet);
}
SELECT * FROM `Users`
if (IpMatch(rowip, pip))
{
// player matched
}
I would recommend a subnet cum CIDR check which i implemented in GAdmin and also you can use IpMatch function:
pawn Код:
pawn Код:
And then compare with player's ip (new pip[18]): pawn Код:
|
GetIPVal(const ip[]) { new ip1, ip2, ip3, ip4; if (sscanf(ip, "p<.>iiii", ip1, ip2, ip3, ip4)) return 0; if (!(0 <= ip1 <= 255) || !(0 <= ip2 <= 255) || !(0 <= ip3 <= 255) || !(0 <= ip4 <= 255)) return 0; return ip1*16777216 + ip2*65536 + ip3*256 + ip4; }
I'll search later on my PC if there's any other way to do that without looping through all rows.
But why do you use that awful way of converting an IP to decimal? You can do something like this (Might have some indentation mistakes cause I'm on my phone) Код:
GetIPVal(const ip[]) { new ip1, ip2, ip3, ip4; if (sscanf(ip, "p<.>iiii", ip1, ip2, ip3, ip4)) return 0; return ip1*16777216 + ip2*65536 + ip3*256 + ip4; } |
static GetIPVal(const ip[])
{
new len = strlen(ip);
if (!(len > 0 && len < 17))
return 0;
new count;
new pos;
new dest[3];
new val[4];
for (new i; i < len; i++)
{
if (ip[i] == '.' || i == len)
{
strmid(dest, ip, pos, i);
pos = (i + 1);
val[count] = strval(dest);
if (!(1 <= val[count] <= 255))
return 0;
count++;
if (count > 3)
return 0;
}
}
if (count != 3)
return 0;
return ((val[0] * 16777216) + (val[1] * 65536) + (val[2] * 256) + (val[3]));
}
stock IpMatch(const ip1[], const ip2[], rangetype = 26)
{
new ip = GetIPVal(ip1);
new subnet = GetIPVal(ip2);
new mask = -1 << (32 - rangetype);
subnet &= mask;
return bool:((ip & mask) == subnet);
}