Call second include if first fails?

Discussion in 'PHP' started by I. Brian, Dec 29, 2005.

  1. #1
    I'm using basic PHP include statements on my sites, and sometimes they call up content from a second website.

    Currently I use something basic like this:

    
    <?php @include("http://www.domain1.co.uk/content.php"); ?>
    
    Code (markup):
    However, I'd like to add some simple PHP code that will, if domain1 cannot be reached, instead call up the same file from domain2.co.uk.

    Any suggestions would be very welcome.
     
    I. Brian, Dec 29, 2005 IP
  2. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try using fopen instead.

    eg

    
    $handle = fopen("http://www.example.com/", "r");
    if($handle){
    echo"$handle";
    }
    else
    {
    $handle = fopen("http://www.example2.com/", "r");
    echo"$handle";
    }
    
    PHP:
    edit: you may need to set the timeout of the first url to be quite short in the php ini file
     
    mad4, Dec 29, 2005 IP
  3. pwaring

    pwaring Well-Known Member

    Messages:
    846
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    135
    #3
    Judging by the documentation on php.net, this bit of code might work:

    if ((include 'http://www.domain1.com/script.php') != 'OK')
    {
       include 'http://www.domain1.co.uk/script.php';
    }
    PHP:
    I haven't used remote includes in my own code though, so I can't guarantee that this will work.
     
    pwaring, Dec 30, 2005 IP
  4. I. Brian

    I. Brian Business consultant

    Messages:
    810
    Likes Received:
    59
    Best Answers:
    1
    Trophy Points:
    145
    #4
    Thanks for the replies - much appreciated - will test both out.
     
    I. Brian, Jan 4, 2006 IP
  5. legend2

    legend2 Well-Known Member

    Messages:
    1,537
    Likes Received:
    74
    Best Answers:
    0
    Trophy Points:
    115
    #5
    use include() instead of just include and do

    if(!@include("file"))
    @include("file2");
     
    legend2, Jan 6, 2006 IP