PHP socket using no-ip

270 views Asked by At

How can I connect via socket using DNS name from no-ip? or Is there a way to get the IP from no-ip dns name?

working

$cnt = socket_connect($socket,"100.100.100.100",$port);

but the IP is dynamic. I need something like:

$cnt = socket_connect($socket, "blablabla.ddns.net",$port);

I´ve tried gethostbyname() but it returns the same IP for all no-ip name.

1

There are 1 answers

3
Jeremy Harris On

This solution would depend on your server, but you can fallback to native operating system capabilities. For example, most Linux installs have glibc, allowing you to run a command like:

getent hosts blahblabla.ddns.net | awk '{print $1}'

Now, PHP functions like exec() and system() can be used to run stuff like this.

For example:

$command = 'getent hosts google.com | awk \'{print $1 ; exit}\'';
$ip = exec($command);

Then use the IP to connect to your socket.

NOTE: Please do not pass user provided input into this unless you want to have a bad time.

REFERENCES: https://unix.stackexchange.com/questions/20784/how-can-i-resolve-a-hostname-to-an-ip-address-in-a-bash-script