Setup trackback - Trackback page

16 August 2009

The trackback page should carry the blog id as a variable. If there is none then we don't want to carry on as the database query may throw back errors.

On the tracback page we want to take the blog id, search the trackback table to make sure that a trackback for this blog id and from this website doesn't already exist (as we don't want spammers attacking us).

If there are no current tracbacks from this website on this particular blog then we will add it to the database and send a message back to the originating website to say that we have added it successfully.

<?php
//Allow trackbacks
$trackbackData = array(
'id' => $_REQUEST['id'], //id of blog receiving the trackback
'title' => "",
'excerpt' => "",
'url' => "",
'blog_name' => "",
'trackback_url' => ""
);

//include class
include "Services/Trackback.php";

//new trackback instance
$trackback = new Services_Trackback();

//set opbject properties
foreach ($trackbackData as $k => $v)
{

$trackback -> set($k, $v);

}

if ($trackback -> receive()) //If this page has received a valid trackback request
{

echo $trackback -> getResponseSuccess(); //Send a success message back to the originating website

//Save trackbacks in database
global $trackbackData;
$postData = $trackbackData['nospam'];
$data = $postData;

include "database/connectdatabase.php"; //Connect to your database
$query = "SELECT id FROM trackbacks WHERE url = '" . $data['url'] . "' AND blog_id = '" . $_REQUEST['id'] . "'";  //Check if this website has sent a trackback request to this blog entry already
$result = mysql_query($query);
if (mysql_num_rows($result) == 0)
{

//save it to database
foreach ($_POST as $key => $value)
{
$$key = addslashes(trim($value));
$string = $string . " " . $key;
}
$query = "INSERT INTO trackbacks (title,excerpt,url,blog_name,trackback_url,blog_id,date) VALUES ('" . $_POST['title'] . "','" . $_POST['excerpt'] . "','" . $_POST['url'] . "','" . $_POST['blog_name'] . "','" . $_POST['trackback_url'] . "','" . $_REQUEST['id'] . "','" . date("Y-m-d H:i:s") . "')";
$result = mysql_query($query);

}

}
else
{

echo $trackback -> getResponseError(); //Send back an error response if it was not successful

}
?>


Once all the code is on the correct pages you will be able to receive trackbacks and then display them on your blog page.

http://www.peternichol.com/entry/trackback/134/

Please leave a comment using the form provided.