[Include] GetServerIP [PHP-Simple Method]
#1

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.
Reply
#2

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.
Reply
#3

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.
Reply
#4

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);
}
Reply
#5

Quote:
Originally Posted by PT
Посмотреть сообщение
I advise you to use strcpy
Thanks for advising, updated again.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)