GetServerIP [PHP-Simple Method] -
n0minal - 27.11.2014
Hmm i was looking for some way to GetServerIP without using bind method and had this idea, basically you can retrieve Server IP by sending an request for a PHP web page, its so simple to use and was made in few minutes.
Pawn Include:
Код:
#include <a_samp>
#include <a_http>
#define HTTP_GET_IP 10
new serverip[32];
strcpy(dest[], const source[], maxlength=sizeof dest)
{
strcat((dest[0] = EOS, dest), source, maxlength);
}
stock GetServerIP()
{
return serverip;
}
forward RequestServerIP();
public RequestServerIP()
{
HTTP(HTTP_GET_IP, HTTP_GET, "anticheater.honor.es/getip.php", "", "OnHttpResponse");
return 1;
}
forward OnHttpResponse(index, response_code, data[]);
public OnHttpResponse(index, response_code, data[])
{
if(response_code ^ 200) strcpy(serverip, "Unknown", sizeof(serverip));
switch(index)
{
case HTTP_GET_IP: strcpy(serverip, data, sizeof(serverip));
}
return 1;
}
forward WhenGameModeInit();
public WhenGameModeInit()
{
return SetTimer("RequestServerIP", 3000, true);
}
#if defined _ALS_OnGameModeInit
#undef OnGameModeInit
#else
#define _ALS_OnGameModeInit
#endif
#define OnGameModeInit WhenGameModeInit
PHP Source:
Код:
<?php
$ServerIP = $_SERVER['REMOTE_ADDR'];
echo($ServerIP);
?>
So now you can use the custom function
RequestServerIP to ask for a ServerIP which will be returned on pubic OnHttpResponse and will format the var "serverip".
PS: The include is using a free hosting, if you want to be sure that php page will works, you can host it by yourself by creating the getip.php file, hosting it by your own and changing the URL on HTTP function:
Код:
HTTP(HTTP_GET_IP, HTTP_GET, "puthereyoururl/getip.php", "", "OnHttpResponse");
#Pastebin for include:
Download (updatedІ)
#Pastebin for PHP Page:
Download
Updates:
-Added constantly checks for server ip automatically (thanks to Lordzy for suggestion).
-Added GetServerIP function which retrieves serverip on a string (thanks to Lordzy for suggestion).
-Added "Unkown" string when server can't connect to php page.
-Added strcpy method to copy HTTP Recieved data to serverip string.
Re: GetServerIP [PHP-Simple Method] -
Lordzy - 27.11.2014
You could have improved this include a lot better. Instead of always doing a check for server's IP, on loading this include, simply store it on an array. So then, GetServerIP can directly return the server IP address.
Secondly, instead of using 'format' function, use 'strcpy' to copy a string. Also I feel that it's better to use a HTTP callback thread of different name (mostly starting with a prefix) as this might be used already.
Re: GetServerIP [PHP-Simple Method] -
n0minal - 27.11.2014
Quote:
Originally Posted by Lordzy
You could have improved this include a lot better. Instead of always doing a check for server's IP, on loading this include, simply store it on an array. So then, GetServerIP can directly return the server IP address.
Secondly, instead of using 'format' function, use 'strcpy' to copy a string. Also I feel that it's better to use a HTTP callback thread of different name (mostly starting with a prefix) as this might be used already.
|
Thanks for feedback, i'll improve this...
@Edit:I'll hook OnGameModeInit to do constantly checks for Server IP.
@Edit2: Updated, thank you for suggestions Mr.Lordzy.
Re: GetServerIP [PHP-Simple Method] -
PT - 27.11.2014
I advise you to use strcpy
Quote:
Originally Posted by Vince
I put this into the string.inc library:
pawn Код:
/* native strcpy(dest[], const source[], maxlength=sizeof dest); */
Function:
pawn Код:
stock strcpy(dest[], const source[], maxlength=sizeof dest) { strcat((dest[0] = EOS, dest), source, maxlength); }
|
Re: GetServerIP [PHP-Simple Method] -
n0minal - 27.11.2014
Quote:
Originally Posted by PT
I advise you to use strcpy
|
Thanks for advising, updated again.