Reciprocal
link check by PHP
Getting reciprocal links is an important approach for
search engine optimization. In order to rank higher in search engine
search results, a web site usually maintains a huge amount of reciprocal
links in its database.
To make reciprocal links update, web masters usually check
these reciprocal links to see if they still have their own web URL
or not. If not they will delete the corresponding reciprocal links
from their database.
Reciprocal link MySQL database
Assume a web master put all his reciprocal links in a
table named RLINKS in MySQL database, which has
column,named backlink,storing reciprocal links.
The job for PHP reciprocal link checking is to pull out
those reciprocal links from database and check them one by one to
see if they still keep your web URL. If the reciprocal links lost
your URL, you delete them from your database.
if you have read the lesson Send
bulk emails in PHP, you will feel familiar with the following code.
Pull out reciprocal links from database
First, you need pull out reciprocal links from MySQL database using
the following code:
//open database connection
$username = "yourname";
$password = "yourpassword"; //input your password here.
$database = "yourdb";
//connect to database
$link=mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("<b>Unable to specified
database</b>");
//retrieve reciprocal links
$query="select backlink from RLINKS";
$result=mysql_query($query) or die('Error, query failed'); |
Check reciprocal link
Now, we need loop through the result set to check existence of your
web URL in those reciprocal links.
$row=0;
$numrows=mysql_num_rows($result);
$myURL="http://wwww.yoursite.com";
//loop through reciprocal links
while($row<$numrows)
{
$backlink=mysql_result($result,$row,"backlink");
$check=linkcheck($myURL,$backlink);
//if reciprocal link doesn't have your url, delete from your
database
if($check==null)
{
mysql_query("delete from RLINKS where backlink='$backlink'")
or die('Error, query failed');
}
$row++;
}
//check reciprocal link function
function linkcheck($mylink,$link)
{
$html = implode('', file($link));
$pos = strpos($html,$mylink);
return $pos;
}
mysql_close($link);
|
The above code use while() loop through the result set. $backlink
get the reciprocal link from result set. function linkcheck is
to check your URL existence in reciprocal link. It converts the reciprocal
link to a string and try to find your URL (another string) in it
by using strpos($html,$mylink);.
If $check is null, it means there is no your URL in reciprocal link.
Then, you delete this reciprocal link from your database by calling mysql_query("delete
from RLINKS where backlink='$backlink'").
Put together
Put the above code together as following and save it as checklink.php(or
whatever name ).
Just remember to change the $myURL to your web site URL. Launch
it in your browser like : http://www.yoursite.com/checklink.php or
you ran it locally: http://localhost/checklink.php. It should do
the trick.
//open database connection
$username = "yourname";
$password = "yourpassword"; //input your password here.
$database = "yourdb";
//connect to database
$link=mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("<b>Unable to specified database</b>");
//retrieve reciprocal links
$query="select backlink from RLINKS";
$result=mysql_query($query) or die('Error, query failed');
$row=0;
$numrows=mysql_num_rows($result);
//change to your URL
$myURL="http://wwww.yoursite.com";
//loop through reciprocal links
while($row<$numrows)
{
$backlink=mysql_result($result,$row,"backlink");
$check=linkcheck($myURL,$backlink);
//if reciprocal link doesn't have your url, delete
from your database
if($check==null)
{
mysql_query("delete from RLINKS where backlink='$backlink'")
or die('Error, query failed');
}
$row++;
}
//check reciprocal link function
function linkcheck($mylink,$link)
{
$html = implode('', file($link));
$pos = strpos($html,$mylink);
return $pos;
}
mysql_close($link);
|
|