Trackback Library

The Trackback Library provides handlers that enable you to send and receive Trackback data. If you are not familiar with Trackbacks you'll find more information here.

Note: As of server version 7.0.6-rc-1 this library does not work on Linux because the write to socket command is still not working in the Linux server engine. On Mac OS X Trackback should work as expected since server version 7.0.1.

Initializing the Library

Like most other libraries in revIgniter, the Trackback library is initialized in your controller using the rigLoaderLoadLibrary handler:

rigLoaderLoadLibrary "Trackback"

Sending Trackbacks

A Trackback can be sent from any of your controller handlers using

rigTrackbackSend(tDataArray)

and code similar to this example:

rigLoaderLoadLibrary "Trackback"

put "http://example.com/trackback/receive/456" into tTBdataArray["ping_url"]
put "http://www.my-example.com/blog/entry/123" into tTBdataArray["url"]
put "The Title of My Entry" into tTBdataArray["title"]
put "The entry content." into tTBdataArray["excerpt"]
put "My Blog Name" into tTBdataArray["blog_name"]
put "utf-8" into tTBdataArray["charset"]

if rigTrackbackSend(tTBdataArray) is FALSE then
  put rigTrackbackDisplayErrors()
else
  put "Trackback was sent!"
end if

Description of array data:

Display Errors

The Trackback sending function returns TRUE/FALSE (boolean) on success or failure. If it fails, you can retrieve the error message using:

rigTrackbackDisplayErrors(openTag, closeTag)

Example:

get rigTrackbackDisplayErrors()

Optionally you can include opening and closing tags (default is "<p>" and "</p>" respectively) like:

get rigTrackbackDisplayErrors("<li>", "</li>")

Receiving Trackbacks

Before you can receive Trackbacks you must create a weblog. If you don't have a blog yet there's no point in continuing.

Receiving Trackbacks is a little more complex than sending them, only because you will need a database table in which to store them, and you will need to validate the incoming trackback data. You are encouraged to implement a thorough validation process to guard against spam and duplicate data. You may also want to limit the number of Trackbacks you allow from a particular IP within a given span of time to further curtail spam. The process of receiving a Trackback is quite simple; the validation is what takes most of the effort.

Your Ping URL

In order to accept Trackbacks you must display a Trackback URL next to each one of your weblog entries. This will be the URL that people will use to send you Trackbacks (we will refer to this as your "Ping URL").

Your Ping URL must point to a controller function where your Trackback receiving code is located, and the URL must contain the ID number for each particular entry, so that when the Trackback is received you'll be able to associate it with a particular entry.

For example, if your controller is called trackback, and the receiving handler is called receive, your Ping URLs will look something like this:

http://example.com/index.lc/trackback/receive/entry_id

Where entry_id represents the individual ID number for each of your entries.

Creating a Trackback Table

Before you can receive Trackbacks you must create a table in which to store them. Here is a basic prototype for such a table:

# Table structure for trackback
# ------------------------------------------------------------

CREATE TABLE `trackbacks` (
 `tb_id` int(10) unsigned NOT NULL auto_increment,
 `entry_id` int(10) unsigned NOT NULL default 0,
 `url` varchar(200) NOT NULL,
 `title` varchar(100) NOT NULL,
 `excerpt` text NOT NULL,
 `blog_name` varchar(100) NOT NULL,
 `tb_date` int(10) NOT NULL,
 `ip_address` varchar(45) NOT NULL,
 PRIMARY KEY `tb_id` (`tb_id`),
 KEY `entry_id` (`entry_id`)
);

The Trackback specification only requires four pieces of information to be sent in a Trackback (url, title, excerpt, blog_name), but to make the data more useful we've added a few more fields in the above table schema (date, IP address, etc.).

Processing a Trackback

Here is an example showing how you will receive and process a Trackback. The following code is intended for use within the controller handler where you expect to receive Trackbacks.

rigLoaderLoadLibrary "Trackback"
get rigLoadDatabase()

if rigFetchSegment(3) is FALSE then
  rigTrackbackSendError "Unable to determine the entry ID"
end if

if rigTrackbackReceive() is FALSE then
  rigTrackbackSendError "The Trackback did not contain valid data"
end if

put rigFetchSegment(3) into tTrackbackDataA["entry_id"]
put rigTrackbackData("url") into tTrackbackDataA["url"]
put rigTrackbackData("title") into tTrackbackDataA["title"]
put rigTrackbackData("excerpt") into tTrackbackDataA["excerpt"]
put rigTrackbackData("blog_name") into tTrackbackDataA["blog_name"]
put the seconds into tTrackbackDataA["tb_date"]
put rigIpAddress() into tTrackbackDataA["ip_address"]

put rigDbInsertString("trackbacks", tTrackbackDataA) into tSQL
get rigDbQuery(tSQL)

rigTrackbackSendSuccess

Notes:

The entry ID number is expected in the third segment of your URL. This is based on the URI example we gave earlier:

http://example.com/index.lc/trackback/receive/entry_id

Notice the entry_id is in the third URI segment, which you can retrieve using:

get rigFetchSegment(3)

Without a valid entry ID, there's no reason to continue. In our Trackback receiving code above, if the third segment is missing, we will issue an error using

rigTrackbackSendError "Your error string."

To validate the completeness of incoming data use:

rigTrackbackReceive()

This function simply looks at the incoming data and makes sure it contains the four pieces of data that are required (url, title, excerpt, blog_name). It returns TRUE on success and FALSE on failure. If it fails you will issue an error message.

The incoming Trackback data can be retrieved using this function:

rigTrackbackData("item")

Where item represents one of these four pieces of info: url, title, excerpt, or blog_name

If the Trackback data is successfully received, you will issue a success message using:

rigTrackbackSendSuccess

Note: The above code processing a Trackback contains no data validation, which you are encouraged to add.

How to Reduce Spam

To make the life of spammers who like to increase their number of inbound links using Trackback without publishing any link to your blog entry a little bit harder you can check if the trackback page has a link to your weblog entry. This is done using:

rigTrackbackHasLink(trackbackURL, entryURL)

Example:

rigLoaderLoadLibrary "Trackback"
get rigLoadDatabase()

if rigFetchSegment(3) is FALSE then
  rigTrackbackSendError "Unable to determine the entry ID"
end if

if rigTrackbackReceive() is FALSE then
  rigTrackbackSendError "The Trackback did not contain valid data"
end if

put rigFetchSegment(3) into tTrackbackDataA["entry_id"]
put rigTrackbackData("url") into tTrackbackDataA["url"]

# CHECK IF TRACKBACK PAGE HAS A LINK TO YOUR BLOG ENTRY
put "http://www.my-example.com/blog/entry/" & tTrackbackDataA["entry_id"] into tLink
if rigTrackbackHasLink(tTrackbackDataA["url"], tLink) then
	
  put rigTrackbackData("title") into tTrackbackDataA["title"]
  put rigTrackbackData("excerpt") into tTrackbackDataA["excerpt"]
  put rigTrackbackData("blog_name") into tTrackbackDataA["blog_name"]
  put the seconds into tTrackbackDataA["tb_date"]
  put rigIpAddress() into tTrackbackDataA["ip_address"]

  put rigDbInsertString("trackbacks", tTrackbackDataA) into tSQL
  get rigDbQuery(tSQL)

  rigTrackbackSendSuccess

else
  # LINK TO YOUR ENTRY IS MISSING, YOU MAY SEND AN ERROR MESSAGE LIKE:
  rigTrackbackSendError "The Trackback page has no link to the corresponding blog entry."
end if