admin管理员组

文章数量:1123105

php version 5.6 is running on a virtual host, so the sleep and usleep functions are disabled, and I don't have permission to enable them. I have to hang the script for a while before I can continue executing it. So far I've thought of using curl or the fsockopen function to send a request that suspends the script. But I don't know what the harm is, how to accurately control the request time, or if there are such sites that only respond in 1-2 seconds. I thought for a day, did not come up with a good way, please help me, sincerely thank you.

php version 5.6 is running on a virtual host, so the sleep and usleep functions are disabled, and I don't have permission to enable them. I have to hang the script for a while before I can continue executing it. So far I've thought of using curl or the fsockopen function to send a request that suspends the script. But I don't know what the harm is, how to accurately control the request time, or if there are such sites that only respond in 1-2 seconds. I thought for a day, did not come up with a good way, please help me, sincerely thank you.

Share Improve this question asked 4 hours ago seansean 531 silver badge3 bronze badges 7
  • Can you execute shell command? shell_exec('sleep 10'); – Barmar Commented 3 hours ago
  • Try with your own sleep(): function simpleSleep($seconds){$timeEnd = time() + $seconds; while($timeEnd > time()) for ($n = 1; $n < 100000000; $n++);}, this should work too: function simpleSleep($seconds){$timeEnd = time() + $seconds; while($timeEnd > time());} – TUPKAP Commented 3 hours ago
  • "and I don't have permission to enable them" - did you ask someone else though? Sometimes it is just a matter of providing a valid reason. "ABC needs feature XYZ, and to do that I need the sleep function which is currently disabled, can you enable it for me?" – Chris Haas Commented 2 hours ago
  • @TUPKAP Sleeping doesn't cost CPU, hammering a loop like that will just needlessly burn cycles. – Alex Howansky Commented 2 hours ago
  • It's not clear why you need the script to sleep. – Olivier Commented 1 hour ago
 |  Show 2 more comments

1 Answer 1

Reset to default 0

Use an webrequest to unknown source

There are several way to use Webrequests for an predigtable Timeout ... So the code below isonly "an Idea" of what to do.

    function curlSleep(
        $sleepSecs = 5,
        $url = 'www.ineverwillreachthis.lostinspace'
    ) {
        $startTime = microtime(true);
        $ch = curl_init($url);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $sleepSecs);

        try {
            $output = curl_exec($ch);
            $endTime = microtime(true);
            if (curl_errno($ch)) {
                echo "cURL error: " . curl_error($ch);
            } else {
                $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                if ($statusCode === 200) {
                    $duration = $endTime - $startTime;
                    echo "Response time: " . $duration . " seconds";
                } else {
                    echo "Server response error with status code: " . $statusCode;
                }
            }
        } catch (Throwable $e) {
        } finnaly {
            curl_close($ch);
        }
    }

本文标签: curlHow to suspend php threads when sleep and usleep functions are disabledStack Overflow