Skip to content

Instantly share code, notes, and snippets.

@konstantinosbotonakis
Created April 16, 2024 12:10
Show Gist options
  • Save konstantinosbotonakis/d8b957f72159b789a8966aeeeafcece4 to your computer and use it in GitHub Desktop.
Save konstantinosbotonakis/d8b957f72159b789a8966aeeeafcece4 to your computer and use it in GitHub Desktop.
Check Timeout on server via PHP
<?php
class TimeoutException extends Exception {}
try {
// Start time
$start = microtime(true);
// Print the full path of the currently executing script
echo "Script loaded from: " . __FILE__ . "<br>\n";
// Set a short timeout for demonstration purposes
declare(ticks=1) {
ini_set('max_execution_time', 2);
}
if (isset($_GET['timeout'])) {
// Sleep for user set seconds
sleep((int)$_GET['timeout']);
} else {
// Sleep for 5 seconds
sleep(5);
}
// End time
$end = microtime(true);
// Calculate the execution time
$executionTime = $end - $start;
echo "Execution time: " . round($executionTime, 2) . " seconds\n";
echo "Script completed successfully.\n";
} catch (TimeoutException $e) {
echo "Script timed out!\n";
} finally {
// Reset max_execution_time to default
ini_set('max_execution_time', 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment