tags:

views:

152

answers:

3

Hello all,

I'm looking for a way to check if a link exists on a certain page. I know that this is possible with a ping, but I really don't know how to do this.

What I have is a list of links to other webpages, they should have a backlink to my page also. I want to check this, when the backlink is there, a text should appear, something like "ok" and when the result is negative, something like "no backlink"

I know the urls of the pages where my link should appear, in case you need to know that.

Any help would be great!

I have found a piece of code wich I think could be used to serve my needs. I self don't know how, but it would be great if anyone can help me with this.

This is the code:

<html>
  <head>
     <script language="javascript" type="text/javascript">
     <!--
        var ajax = new XMLHttpRequest();

        function pingSite() {
           ajax.onreadystatechange = stateChanged;
           ajax.open('GET', document.getElementById('siteToCheck').value, true);
           ajax.send(null);
        }

        function stateChanged() {
           if (ajax.readyState == 4) {
              if (ajax.status == 200) {
                 document.getElementById('statusLabel').innerHTML = "Success!";
              }
              else {
                 document.getElementById('statusLabel').innerHTML = "Failure!";
              }
           }
        }
     -->
     </script>
  </head>

  <body>
     Site To Check:<br />
     <input type="text" id="siteToCheck" /><input type="button"  onclick="javascript:pingSite()" />

     <p>
        <span id="statusLabel"></span>
     </p>
  </body>
A: 

You can't natively use Javascript to parse external domains, I used a proxy page which sniffs the content and feeds it to the ajax callback.

My solution basically grabs the source of the site to check and sees if a string, which can be your site link matches. I would assume this should be sufficient rather than trying to parse and look for anchors, but you can be as thorough as you want ( parse the whole thing as a DOM element and look for href attribute value ).

Let me know if you run into any issues.

<?php
    $query = isset($_POST['submitted']) ? true : false;
    if ( $query ) {
    $url = @file_get_contents( $_POST['site-url'] );
    if ( $url && strlen( $url ) > 0 ) {
        $checkFor = $_POST['check-for'];
        $match = preg_match( '/' . $checkFor . '/', $url );
        echo $match ? 'string (link) is found' : 'string not found';
    } else {
        echo 'could not connect to site..';
    }
    exit;
    }
?>
<form action="" id="site-checker">
    <div class="field">
    <label for="site-url">Site to check:</label>
    <input id="site-url" name="site-url" value="http://jquery.com"&gt;
    </div>
    <div class="field">
    <label for="check-for">Check for:</label>
    <input id="check-for" name="check-for" value="docs.jquery.com">
    </div>
    <input type="hidden" name="submitted" value="true">
    <input type="submit">
</form>

<div id="result"></div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"&gt;&lt;/script&gt;
<script>
    $('#site-checker').submit(function(e) {
    e.preventDefault();
    $.ajax({
        url:'check.php',
        type:'POST',
        data:$('#site-checker').serialize(),
        success:function(html) {
     $('#result').html( html );
        }
    });
    });
</script>
meder
I tried your sollution meder, but I didn't show negatives or positives. Do you know why? It looks like there is nothing is happening.
Chris
Did I exidently delete an other post of yours Meder?
Chris
Can you please post more information on what you tried. This works *fine* on my local Apache server.
meder
ok, I get this in some cases. Warning: preg_match() [function.preg-match]: Unknown modifier '/' in /var/www/g35003/coldcharlie.nl/HTML/check.php on line 7 string not found
Chris
Well, for the most part it works, no? And I think it covers more than what you asked for your question. It might actually help if you told me in what circumstances it throws any error.
meder
I just tsted a few and it works great, no errors. I don't know why it gave errors before but now it's ok. something to do with googleapis?Great help! thanks!
Chris
A: 

IMHO it would be better to perform this task in a server side script. Depending on your platform there might be functions for sending HTTP requests and HTML parsing which is what you need here. Javascript has cross domain restrictions which will prevent you from sending ajax requests to different domains than the one that is hosting the web page.

Darin Dimitrov
Can you give a example please?
Chris
In what language/platform?
Darin Dimitrov
Ajax for example? in something Meder did?
Chris
http://www.coldcharlie.nl/test.phpsite to check: http://pc.startkabel.nlcheck for: http://friesecomputerservice.nl
Chris
Um. In *my* code the page name was 'check.php', your page name is 'test.php' therefore look through my code and change 'check.php' to your filename.
meder
And can you comment in my answer? I didn't see it because it was under darin's answer.
meder
A: 

hello! i need a sample like this, but after do the test, save the web site posted in a mysql

Leo lima