1

I wanted to redirect user to a remote server after processing is done on my server. Sometimes, because of network connection timeout in user's end, the redirection isn't happening which causes his/her page to not get updated status.

What I currently use is

header('Location: http://anotherwebsite.com');

If this fails, it will not try again... how can I implement something that 'will try again'

$retry_limit = 0;
while(//http status code not 301 or 302 && $retry_limit < 3)
{
    header('Location: http://anotherwebsite.com');

    $retry_limit++;
}

I'm confused if I use cURL then it will double redirect if I also implement header... or maybe I misunderstood it?

Thanks a lot!

Woppi
  • 5,303
  • 11
  • 57
  • 81
  • check this out maybe it can help http://stackoverflow.com/questions/2964834/php-check-if-url-redirects in particular the answer here http://stackoverflow.com/a/2965116/1202367 – ExoticSeagull Feb 27 '13 at 08:58
  • Isn't the info available in the backtrace? [php debug_backtrace](http://php.net/manual/en/function.debug-backtrace.php) – Ron van der Heijden Feb 27 '13 at 08:59
  • You should really ask the remote server operator to improve on their uptime... – pixelistik Feb 27 '13 at 09:24

2 Answers2

1

As has been pointed out, header() will just fire a HTTP header and forget, so with PHP you won't be able to easily implement a retry mechanism.

But what is the underlying problem that you are trying to fix? If the partner website that you are redirecting to is so overloaded that it will sometimes only react on the 2nd or 3rd attempt: seriously, you should make that server work more reliably.

On the other hand, if you are simply looking for a way to notice a possible downtime of the other server and inform your users accordingly, you could add a quick server-to-server check to your code. In case the other server is down, you can redirect to a different page and apologise or offer a retry link.

Look at this answer for a way to ping a server to find out if it is up or not.

A rough solution could look like this:

<?php
$url = 'http://anotherwebsite.com';

if(pingDomain($url) != -1) {
    header('Location: ' . $url);
} else {
    header('Location: sorry_retry_later.html');
}

// Ping function, see
// https://tournasdimitrios1.wordpress.com/2010/10/15/check-your-server-status-a-basic-ping-with-php/
function pingDomain($domain){
    $starttime = microtime(true);
    $file      = fsockopen ($domain, 80, $errno, $errstr, 10);
    $stoptime  = microtime(true);
    $status    = 0;

    if (!$file) $status = -1;  // Site is down
    else {
        fclose($file);
        $status = ($stoptime - $starttime) * 1000;
        $status = floor($status);
    }
    return $status;
}
Community
  • 1
  • 1
pixelistik
  • 7,541
  • 3
  • 32
  • 42
  • Thank you for the idea of pinging the remote server first before redirecting. I'll also apply it. I've come up with another solution wherein I can apply a retry. That is to have a "notify url" implemented on our client's site which will pass the same data as in the return url, then redirect to return url once done. Hmmmm that's the only way I know right now that can do retry... Do you think it could also somehow eliminate frequent network timeout problems? – Woppi Feb 28 '13 at 00:54
0

header can be only used for one-time redirects. Since there is no return value, you can't check it this way. You should first try with JSON to check if there is a response from the site, if so, redirect the user, else write an error message or something.

Reference for JSON

I haven't used this personally, but have seen others do it successfully with this method.

PeterInvincible
  • 2,230
  • 5
  • 34
  • 62
  • Thanks but the script is on the server side and not on client-side. Yes it would be easy via javascript. I haven't tried in PHP. – Woppi Feb 27 '13 at 09:06
  • Do it this way: When the user submits the form instantly return false with js, then, in the same event check with JSON if the other site is available, if it is return true. After this the form will be submitted, and PHP will do the rest. So basically before the submitting you check the availability. – PeterInvincible Feb 27 '13 at 09:21
  • Hi, yes via js it can be done however the case is not just a simple form then PHP, it's APIs communicating with each other server-side. Thanks for taking time though. I appreciate it. :) – Woppi Feb 28 '13 at 00:47