Dynamic Relevancy Message Generation for Links Using PHP

By Meitar Moscovitz (first published March 11th, 2005)

The Necessary Background

If you haven't read it yet, check out my previous relevant experiment for some important context. Then come back here. Don't worry, it's really short.

The Concept

Now, let's say you'd like to let people insert their own text to cite the relevance of why they've linked to you. I call these snippets of text relevancy messages, and their significance is that they appear on the destination page rather than in the link. They are supposed to be used to link to fragment identifiers (named anchors) in pages, especially long pages. They can be styled willy-nilly via CSS, just like any other element.

The problem with relevancy messages using the aforementioned technique (see my blog entry for details, or the above-linked experiment) is that you need access to the original HTML document in order to embed them. However, using a very simple API such as the one I've implemented here allows anybody to write their own relevancy messages and use them on your pages by writing specially-crafted links.

The Technical Implementation

Super-simple: just grab and verify (escape) the string within a special GET querystring variable. You merely need to urldecode() it, escape any potentially harmful data, and then plug it into your style sheet.

Proof of Concept

This page is my proof-of-concept. Follow the following link(s) with a CSS2-compliant browser (such as Safari, Netscape 7, Mozilla, or Firefox, but not any version of Internet Explorer) to see the special message. Then examine the address bar in your browser.

See the r-msg=text+here part right before the # and the last bit? The last bit is the anchor you you clicked on, and the text+here is the relevancy message. The key to this message is that, in order to be able to embed it into a URI, it needs to be urlencoded() by the author. Most web-monkeys know what I'm talking about.

Anybody, including you, can change the r-msg value to whatever you'd like (provided it is urlencode()'ed or escape()'ed) to insert your own relevancy message. You can then use this special URL in a link on your site, in instant message conversation with your friends, or anywhere else where you can type HTML or web addresses.

Here's the code I used here:

<?php
if ($_GET['r-msg']) $msg = htmlspecialchars(urldecode($_GET['r-msg']));
else $msg = 'Look right here:';
?>

[…]

<style type="text/css"><!--
*:target::before { content: "<?php echo $msg?> "; font-size: larger; font-weight: bold; color: green; }
--></style>
        

Still Limited by the Anchors

Of course, this still requires that the original author properly tag his document's elements appropriatly. Of course, from here, it's just a short jump to scripting that process as well and giving linkers the chance to specify which element they'd like to reference. ;) More on that, hopefully, soon.