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;
}