How to check if a link still exists and if it can open in an iframe

1

I use iframe in my project to open certain links from different sites! These links are stored in a database, but they make a future progression that same links can be changed or deleted, soon they cease to exist, so I would like to know if you can check if the link still exists.

EX: I registered the link today in BD and for a month the link became active, after that month the link was disabled for some reason, this will generate a small inconvenience in my project! I would then like to check if the link I have registered is still active whenever it is requested in BD .

If you do not have a iframe link in the iframe , there are some sites that block this rendering within iframe . Google and Twitter themselves are examples of this. I would like at the same time to check the link the script would check if the link allows to be opened within an iframe and if the link does not exist anymore or it can not be opened in an iframe / em> I would like you to redirect the page to another informing you that the link does not exist!

    
asked by anonymous 05.01.2015 / 03:25

1 answer

3

The simplest and most straightforward way to test whether a URL is "working" is to make a request and check the returned HTTP status. Anything other than 200 , such as error 400 (not found) or 500 (server error), means a problem.

I can tell you that it will probably not be possible to do this through the browser. There is a big problem, which is the security policy that does not allow Ajax requests to other domains via JavaScript.

In fact, the security restriction can be circumvented using CORS , however the pages accessed in iframe s would have to give explicit permission to your domain.

A workaround is to create a service on your own server that requests the URL that needs to be tested and then returns the HTTP status, thus working as a kind of proxy .

One way to implement this would be to set the src of iframe to a special page that checks the availability of the URL and then makes a redirect.

Assuming you use PHP on the server (it can be any language), you could then create a page called validar-url.php . This page is parameterized as an identifier for the destination URL. You could then set a iframe by pointing to this page, like this:

<iframe src="validar-url.php?id=123">

Then on your page validar-url.php would have a logic something like this:

$url = recuperarUrl($_GET['id']);
$status = verificarUrl($url);
if ($status == 200) {
    header("Location: $url");
} else {
    header("Location: pagina-de-erro-quando-url-nao-disponivel");
}

You could also use the service with JavaScript via Ajax if you want to implement logic in the browser.

    
05.01.2015 / 16:41