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

Asked

Viewed 1,207 times

1

I use iframe in my project to open certain links from different sites! These links are stored in a database, but make a future progression these same links can be changed or deleted, soon they no longer exist, so I wonder if there is a way to check if the link still exists.

EX: I registered the link today on BD and for a month the link was 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 registered is still active whenever it is requested on BD.

Here comes the other part, as I commented on the question, I open the links on iframe, only that there are some sites that block this rendering within the iframe, Google and Twitter themselves are examples of this. I would like you to check the link at the same time that the script checks whether the link allows it to be opened inside a iframe and if the link no longer exists or it cannot be opened in a iframe I would like you to redirect the page to another stating that the link does not exist!

  • Ivan, you want to do this in the browser via Javascript or on the server side (back end)? In the second case, what platform/language is using?

  • I’d like to do it for the same browser!

1 answer

3


The simplest and straightforward way to test if a URL is "working" is to make a request and check the returned HTTP status. Anything other than 200, as an 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, but the pages accessed in iframewould have to give explicit permission for your domain.

An alternative solution is to create a service on your own server that makes a request in the URL that needs to be tested and then returns the HTTP status, working as well as a kind of proxy.

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

Supposing that you use PHP on the server (can be any language), could then create a page called validar-url.php. This page takes as a parameter an identifier for the destination URL. It could then define a iframe pointing to that page, like this:

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

Then on your page validar-url.php would have a logic more or less 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.

  • I would also need to check if the page returns that header that prevents it from being used in iframe (I don’t remember the exact header).

  • @bfavaretto Good! It’s the X-FRAME-OPTIONS, right?

  • 1

    302 would also not be a valid HTTP status for it ? if it is using a redirect.

  • 1

    @Murilogambôa It depends on the use case, but your comment is pertinent. I believe that a real implementation would have to consider the different possibilities as 301, 303, 304 and so on. However, not all these states could always be considered successful in this context, after all it may be that the system does not want to take into account redirects, to know if it is a real page.

  • I understand, thank you very much :3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.