0

I am trying to write php to refresh the url redirect. The saturation is if destination page is busy, it will redirect to a url like http://www.go.com/busy.php. If it goes normal, it will go to http://www.go.com. And I want to refresh the web until it goes to the normal page.

Here is my code but I think it's wrong...

<?php

$url = "http://www.go.com/busy.html";
$url2 = "http://www.go.com/action.php";
$page = $_SERVER['PHP_SELF'];
$sec = "1";


if ($url == $url2) {
   header ("http://www.go.com/action.php");
} 
else {
   header ("Refresh: $sec; url=$page");
}
?>
Sunman
  • 9
  • 1
  • Possible duplicate of [How to make a redirect in PHP?](https://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php) – MrSmile Aug 03 '18 at 07:02
  • Welcome to StackOverflow. You need to be clear about what is happening - 'I think it's wrong' is too general, for when you post future questions. If you are trying to override the busy page, I suspect the target website has your number (i.e. IP address/session) and is basically pushing you away all the time - i.e. your code works but the target site has more power over this than you do! – MandyShaw Aug 03 '18 at 07:07
  • Sorry for lack of information. I am new here. "Wrong" is about my code. That's not related to IP. We have to queue to access the site. We have to refresh until the web release a session for me. After the web release the session for me, i will go http://www.go.com normally. When the web doesn't allow me to access, the web redirects me to http://www.go.com/busy.php. So I have to refresh again until i can get in http://www.go.com – Sunman Aug 03 '18 at 07:12

1 Answers1

1

I believe what you're asking is how to hold a user on a page until the remote server response time meets a threshold.

You could do a cURL request to the first URL to see if it meets the timeout requirement. If it fails then redirects to the new URL.

This is untested:

<?php
$urlToCheck = "http://www.example.com/";
$maxAllowedRequestTime = 3; // Seconds

$ch = curl_init($urlToCheck);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_exec($ch);
$requestTime = curl_getinfo($ch, CURLINFO_TOTAL_TIME);
curl_close($ch);

// Allow redirect because timeout is less than or equal to $maxAllowedRequestTime
if (false !== $requestTime && $requestTime <= $maxAllowedRequestTime) {
   header ("Location: $urlToCheck");
} else {
   // Refresh current page until request time is less than or equal to $maxAllowedRequestTime
   header ("Refresh:1");
}
Jason Grim
  • 413
  • 4
  • 7
  • HI jsonArray, I am testing on the program but seems not working. It seems didn't refresh the page and hold on the redirected page. Let me try to edit the code. Thanks~ – Sunman Aug 06 '18 at 01:17
  • @Sunman I just tested locally and it appears to be working for me. I may have misunderstood your question. I assumed you wanted a landing page to keep reloading waiting for another URL to not be a busy. This script will try to load the destination url ($urlToCheck) and see the response time. If it's more than the threshold `$maxAllowedRequestTime` in seconds then it reloads and tries again. – Jason Grim Aug 06 '18 at 04:42