0

Possible Duplicate:
Detecting if youtube is blocked by company / ISP

Is there a way to test if a user's browser has access to YouTube servers using JavaScript or PHP?

Some companies block access to certain sites, like YouTube for obvious reasons, and therefore it's necessary to stream fallback videos from a different CDN if that is the case. I currently have a solution using ActionScript, but I would prefer to use PHP or JavaScript to replace the div instead if that's possible.

Community
  • 1
  • 1
CarlosM
  • 79
  • 7

1 Answers1

1

EDIT:

As @NathanKleyn said the php code below wil only check if your server has access to youtube, not the client that's using your tool. If this is what you want (which i guess it is after re-reading your question) the javascript solution below should be a solution too.

var request = new XMLHttpRequest();
request.open('GET', 'http://www.youtube.com', false);
request.send(null);
alert(request.status); 

One way to achieve this is to request the headers on youtube.com with PHP's get_headers(), check if the HTTP code returned to determine if the site is accessible.

You could probably do this with curl too though it is more complex, yet alot faster.

ChrisR
  • 14,370
  • 16
  • 70
  • 107
  • 1
    http://hyperboleandahalf.blogspot.com/2010/04/alot-is-better-than-you-at-everything.html – Michael B Nov 08 '11 at 21:04
  • loool @KyleBoddy ... yet since alot isn't a valid english word there can't be any confusion so i'll use it alot anyway ... :D – ChrisR Nov 08 '11 at 21:09
  • Thanks, ChrisR. This is a very reliable solution. – CarlosM Nov 08 '11 at 22:14
  • 1
    I'm assuming you have the PHP running on each of the user's computers? If not, you're only testing your server's access to YouTube by doing this, not the end-user's. – Nathan Kleyn Nov 08 '11 at 23:25
  • Hey Chris, I'm having trouble implementing this solution. Can you assist me in generating an alert with an error example. Thanks, – CarlosM Feb 14 '12 at 19:17
  • This solution will not work on all modern browsers.. For example, chrome and safari block by default the crossdomain requests... – Guilherme Oliveira Aug 01 '13 at 20:43