I am thinking of setting up a link shortener for myself, and I want to know how to track unique and total clicks.

Well, the total clicks are easy to do. I think the unique clicks are more challenging.

Also, how do you do 301 and 307 redirection? Is this code correct?

header('HTTP/1.1 301 Moved Permanently');
header('Location: http://www.example.com');
exit();
header('HTTP/1.1 307 Temporary Redirect');
header('Location: http://www.example.com');
exit();

your advice/ideas/code snippets are welcome.:)

Regards,

John

You can add Google Analytics (or any other tracking tool of course) to your site.

I found this to be an interesting article on redirecting.

Yes, the codes for 301 & 307 are right.

To track unique and total clicks on a link in PHP, you can try this:

Mysql:

CREATE TABLE ip (ip VARCHAR(255) NOT NULL);

$ip = $_SERVER;
// connect to db
$query = mysql_query("SELECT * FROM ip WHERE ip='$ip';");
$result = mysql_fetch_array($query);
if (isset($result[ip]) || $result[ip] != $ip){
mysql_query("INSERT INTO ip (ip) VALUES ('$ip');");
}