I need some help I am trying to terminate my script if it reaches to 60 seconds if it can't connect to the server,but I always caught by 60 seconds limit execution time.I think my code is not working.
<?php
$time_limit = 60;
set_time_limit ($time_limit);
if(isset($_GET['comm'])){
$command = $_GET['comm'];
$host = "xxx.xx.xxx.xx";
$port = xxxx;
$start_time = time();
$message = $command;
$socket = socket_create(AF_INET, SOCK_STREAM,0) or die("Could not create socket\n");
while(!@socket_connect($socket, $host, $port)){
if ((time() - $start_time) >= $time_limit)
{
socket_close($socket);
die("Connection timed out.\n");
}
sleep(1);
continue;
}
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
$resultserv = socket_read ($socket, 1024) or die("Could not read server response\n");
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://tomysite/receive.php',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'recrespond' => $resultserv
)
));
$resp = curl_exec($curl);
curl_close($curl);
echo $resultserv;
socket_close($socket);
}
?>
Thank you in advance.