PDA

View Full Version : How can I monitor what links my surfers are clicking



wkenny
10-06-2005, 06:00 PM
My site is a directory showing business contact details including web links.

Each link is displayed within <td></td> tags and target is _blank.

I would like to know what external links users may be clicking on.

Is there any way to store what they've clicked on e.g. the href and date, to a simple text file on my site?

Twey
10-06-2005, 07:46 PM
Yes. You need a "redirect" page, which should be invisible to the user.

// redir.php
$log = fopen("/ws.log", "a");
fputs($_SERVER['HTTP_REMOTE_ADDR'] . ' accessed ' . $_GET['url'] . ' on ' . date("g:i, D") . ' the ' .date("dS") . ' of ' . date("F") . ' from ' . $_SERVER['HTTP_REFERER'] . "\n", $log);
fclose($log);
header("Location: http://www.mysite.com/" . $_GET['url']);
Link to it like so: redir.php?url=pages%2fpagetogoto.htm to go to http://www.mysite.com/pages/pagetogoto.htm.
%2f, by the way, is URL-talk for '/'.

wkenny
10-06-2005, 10:22 PM
Thanks Twey.

I'm afraid I'm not too clear on the use of this.

If the link on mysite.com reads:

<a href="theirsite.com" target=_blank>See their site</a>

I should change this to:

<a href ="redir.php?url=theirsite.com" target=_blank>See their site</a>

and in redir.php I should use header ("Location: $_GET['url']")
to go on to theirsite.com

Twey
10-07-2005, 07:44 AM
Ah, yes, sorry. I was thinking only in terms of pages on your site. What you suggest will indeed work.

wkenny
10-07-2005, 08:44 AM
Brilliant! Thanks again, Twey

wkenny
10-18-2005, 03:41 PM
I've uploaded amended pages and now call the script ok.
I had to change it slightly to get it to work (by putting the log filename as the first parameter).

My version is

<?php
$log = fopen("ws.log", "a");
fputs($log,$_SERVER['HTTP_REMOTE_ADDR'] . ' accessed ' . $_GET['url'] . ' on ' . date("g:i, D") . ' the ' .date("dS") . ' of ' . date("F") . ' from ' . $_SERVER['HTTP_REFERER'] . "\n" );
fclose($log);
Header ("Location: " . $_GET['url']);
?>

However, the remote_addr is not being written to the log. This is a line from the log:
accessed http://www.eurobrokers.org on 12:15, Sat the 15th of October from http://www.estateagentsespana.com/costadelsol/benalmadena_estate_agents_01.htm

Any idead why?

Twey
10-18-2005, 04:39 PM
I beg your pardon, it's not HTTP_REMOTE_ADDR, just REMOTE_ADDR.
I've made far too many errors on this thread. Time to brush up my PHP again :p

wkenny
10-18-2005, 05:09 PM
As ever, your help is greatly appreciated, Twey.

As for errors, once we get there in the end do they matter? :p