how to find alternate url

Discussion in 'PHP' started by lusiano, May 15, 2006.

  1. #1
    hi all,
    i have a minor problem because i'm not very good at php
    let say that i'm running a webcam for my office.
    today i came to my office and start my computer, at the computer installed a webcam that every 5 second capture the picture of me and saving the file to some folder and file, let say that the folder called webimages at somedomain.com and the filename called images.jpg. so, to view the image we go to this url http://somedomain.com/webimages/images.jpg.

    the problem is when i turn my computer the link will go down (i have to turn off the computer)

    how to make some script to show another url if the link is down

    some thing like this

    <img src="http://somedomain.com/webimages/images.jpg" />

    i turn off the computer and the link will go down
    after the computer is off i want the link to show to other url
    like

    <img src="http://otherdomain.com/blankimages.jpg" />

    how to do this is PHP?

    thank you
     
    lusiano, May 15, 2006 IP
  2. sketch

    sketch Well-Known Member

    Messages:
    898
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    148
    #2
    Use PHP's fsockopen function to see if a specific server responds:

    @$fp = fsockopen ( $domain, $ports[$portNames[$y]], $errNum, $errMsg, $timeout);
    if (!$fp) {
    	// server did not respond
    	// add whatever code you want to run in this case
    } else {
    	fclose($fp);
    	// server DID respond
    	// add whatever code you want to run in this case
    }
    Code (markup):
    The @ prevents an fatal error from being displayed on your page. The fclose() is just to disconnect for neatness's sake :)
     
    sketch, May 15, 2006 IP
  3. lusiano

    lusiano Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    whoa, i still dont understand......
    i'm sorry for my incapability.
     
    lusiano, May 15, 2006 IP
  4. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ini_set (default_socket_timeout, "3");
    error_reporting(0);
    $theurl="http://somedomain.com/webimages/images.jpg";
    $filestring=file_get_contents("$theurl") ;
    if($filestring){
      echo"$filestring";
    }
    else{
    echo"http://somedomain.com/alternateimage.jpg
    }
    PHP:
     
    mad4, May 15, 2006 IP
  5. lusiano

    lusiano Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you mad4, its working...
     
    lusiano, May 15, 2006 IP
  6. sketch

    sketch Well-Known Member

    Messages:
    898
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    148
    #6
    Oh, my mistake... I thought you wanted to serve an image from an alternate server :D that's what my code does.
     
    sketch, May 15, 2006 IP