How can a PHP script establish a UDP connection with a Gameserver and determine if it is online or offline?

To establish a UDP connection with a Gameserver and determine if it is online or offline, you can use PHP's socket functions to create a UDP socket and send a simple message to the Gameserver. If the Gameserver responds, it is online; if there is no response, it is offline.

<?php
$server_ip = 'gameserver_ip';
$server_port = 'gameserver_port';

$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_sendto($socket, "Hello", strlen("Hello"), 0, $server_ip, $server_port);

socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 1, 'usec' => 0));
$buf = '';
$from = '';
$port = 0;
socket_recvfrom($socket, $buf, 512, 0, $from, $port);

if ($buf) {
    echo 'Gameserver is online';
} else {
    echo 'Gameserver is offline';
}

socket_close($socket);
?>