-1

I need to redirect to an unknown address. If the address is not available I would like to show a message to the user. How to do that?

<?php
header("Location: http://www.example.com/"); 
exit;
?>
Blazemonger
  • 90,923
  • 26
  • 142
  • 180
Matt
  • 2,981
  • 5
  • 23
  • 33
  • 2
    Define "not available" – Sebas May 09 '13 at 13:49
  • How is it possible that you do not know the address? – usumoio May 09 '13 at 13:49
  • 2
    Test the website first with `file_get_contents`. Then show an error if your website can't connect. – Dave Chen May 09 '13 at 13:49
  • In the same spirit as @Dave but with probably better performance http://stackoverflow.com/questions/9841635/how-to-ping-a-server-with-php – Mihai Stancu May 09 '13 at 13:50
  • Where is the address coming from? `get`? If so, have you considered possible problems with that. I.e. me redirecting users to `http://virusinfestedsite.com`? – PeeHaa May 09 '13 at 13:50
  • @DaveChen's solution isn't perfect, but the only you can get. Once the browser followed the redirect to somewhere off-site, you have no control any more. – Lukas May 09 '13 at 13:50
  • 1
    ah ping, but the web server could still be down. – Dave Chen May 09 '13 at 13:51
  • 1
    see http://stackoverflow.com/questions/2280394/check-if-an-url-exists-in-php – worenga May 09 '13 at 13:53
  • 1
    @mightyuhu best solution, checks headers as well – Dave Chen May 09 '13 at 13:56
  • I believe what is happening here is that you have a dynamic URL to which you require to redirect ONLY if the URL exists - else you have to give an error message. You can use cURL or what @Blazemonger suggested for this. cURL helps to know if the resource is available by checking the header of the response. You can [find an example here](http://stackoverflow.com/questions/1722613/check-if-a-remote-page-exists-using-php) Btw, that kind of makes this a duplicate question :) – raidenace May 09 '13 at 13:53

3 Answers3

2

The most direct method is to just retrieve the page:

if (file_get_contents('http://www.example.com/') !== false) {
  header("Location: http://www.example.com/"); 
  exit;
}

http://php.net/manual/en/function.file-get-contents.php

However, this only tells you if SOMETHING is available at that page. It won't tell you if, for instance, you got a 404 error page instead.

For that (and to save the memory cost of downloading the whole page), you can just get_headers() for the URL instead:

$url = "http://www.example.com/";
$headers = get_headers($url);
if (strpos($headers[0],'200 OK') !== false) { // or something like that
  header("Location: ".$url); 
  exit;
}
Community
  • 1
  • 1
Blazemonger
  • 90,923
  • 26
  • 142
  • 180
0

You can check if url exist then redirect:

$url = 'http://www.asdasdasdasd.cs';
//$url = 'http://www.google.com';

if(@file_get_contents($url))
{
    header("Location: $url"); 
}
else
{
    echo '404 - not found';
}
Siamak Motlagh
  • 5,028
  • 7
  • 41
  • 65
0

You can do it using curl

      $ch = curl_init(); 
      curl_setopt($ch, CURLOPT_URL, "http://www.example.com/"); 
      $output = curl_exec($ch); 
      if(curl_errno($ch)==6)
        echo "page not found";
     else
      header("Location: http://www.example.com/");      

       curl_close($ch);  
Shijin TR
  • 7,516
  • 10
  • 55
  • 122