
ShoeMoney did a video post about monetizing blogs. He’s rather critical of AdSense (has a fair few points too) but he did offer one piece of advice which I had been meaning to put in place for a while, and that is to use a redirection script to send people off to affiliate links as opposed to direct-linking them. So I went away and built a custom one for GO Blog.com.au, and thought I would share the code with everyone.
Save the following code as something like /url_redirect.php
<?php
if(!isset($_REQUEST[’to’]))
$toURL = “http://www.goblog.com.au“;
else
$toURL = $_REQUEST[’to’];
$time = time() ;
if (!get_magic_quotes_gpc()) {
$toURL = addslashes($toURL );
}
$db = mysql_connect(”localhost”, “goblog_urlredirect”, “dvs-dv8″);
if (! $db)
die(”Couldn’t connect to MySQL”);
mysql_select_db(”goblog_urlredirect”) or die(”Could not access database: “.mysql_error());
mysql_query (”INSERT INTO toURL (url, time) VALUES (’$toURL’, ‘$time’)”);
mysql_close($db); header (”Location: $toURL”);
?>
What does it do? It obviously provides a little more than just a nice redirect. It also allows me to monitor the number of users who follow those links (and with the date stamp it will let me sort over time). You will need a MySQL database set up with the something similar to the following table:
CREATE TABLE `toURL` (
`id` int(11) NOT NULL auto_increment,
`url` longtext NOT NULL,
`time` int(11) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
Now any links to /url_redirect.php?to=http://google.com.au will be stored in your database and the user will be directed to the appropriate external link like normal.
If something is confusing for you, let me know and I’ll try and explain. Enjoy!




Mark responded on 06 Mar 2008 at 11:07 am #
That code is vulnerable to SQL injection. You should clean up the $toURL before simply placing it in the SQL command.
Andrew responded on 06 Mar 2008 at 1:02 pm #
And that’s why I should stick to doing frontend design ;-).
But correct me if I’m wrong, having Magic Quotes on in your PHP server settings should prevent SQL injection anyway? I’ve added a conditional statement to check, and if it’s not on, do some cleaning now anyway.
Mark responded on 06 Mar 2008 at 2:22 pm #
Magic Quotes on should work for text fields but there are some vulnerabilities with database and file insertions I seem to recall. Also, I only mentioned it originally because yes, you may have magic quotes on, but if someone else reading this doesn’t and then adds the code to their site then they might be opening themself up to attack. However, your code adjustment now should help.
From a personal point of view I hate using redirection scripts in this manner, i.e. taking the URL as a parameter and redirecting there. My preferred method when I’ve used this in the past was to maintain a database of redirection links so that the redirection was of this form instead:
/redirect.php?id=100
The benefit of this system is that it’s easier to validate a number, you could, at a later date, alter all the redirection links site-wide if, for example, the place you’re redirecting to changes their URL (has happened), and more importantly, it stops someone else bouncing off your page to someone they want to visit, faking the referrer to be your site.
On the downside, who wants to maintain a list of links? Nobody. Which is why I implemented a helper piece of code. Effectively, in the PHP of your page you’d replace any link with a piece of code:
from: visit this site
to: visit
the helper function linkto() would check to see if “somesite” was already in the database. If so then it gets replaced with:
this site
otherwise, “somesite” is added and
this site
is output to the page instead.
It relied on you being able to add PHP to a page but it was in use on a number of websites a few years ago for tracking purposes.
Andrew responded on 06 Mar 2008 at 2:32 pm #
That’s a much more elegant solution.
I currently use the /redirect.php?id=100 method on other sites, (keyword-based actually, instead of numeric) but hadn’t thought of your suggestion to simplify the painful problem of the list compilation.
Thanks for your contribution, it’s appreciated.