Does anyone know some PHP code I could use to test if a website is down and if it is then redirect the current page to a designated page?

Basically the following flow needs to happen:

index.php is visited. As it opens up it check to see if site A is up. If so it proceeds to site A, if not it redirects to site B.

The redirection part is not a problem, I'm just needing some ideas on testing for the invalid or not working site.