Forum Moderators: coopster

Message Too Old, No Replies

counting clicks through PHP - link redirect

PHP click counter and redirect

         

smallcompany

8:07 am on Nov 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



We use a php file to store outgoing links.Within HTML page such link looks like this:

../links.php?id=link10

links.php is like this:

$time = date("Y-m-d h-i", time());
$ip = $_SERVER['REMOTE_ADDR'];

if ($_GET['id'] == "link10") {$link = "http://www.site10.com";}
if ($_GET['id'] == "link11") {$link = "http://www.site11.com";}

At the bottom of the file we have:

header("Location: $link");

To this $link we can append information like time, IP address, etc. (as shown, $time, $ip)

All works fine.

Now we would like to log each click where we would write information into flat file like csv.

Information that would be written is a click numeric order, time, IP address, and link ID – four columns in total.

If some of php experts is favouring mysql, it’s ok, we can use that as well.

The question is:

What would be the shortest code within existing link.php file that would do this task?

As optional function we could also use click number as a variable that would be appended at the end of outgoing link, something like this:

header("Location: $link+$cn");

where $cn is a click number being constantly increased by value of 1.

Thanks

PHP_Chimp

9:01 am on Nov 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to log as a flat file then you could use the fopen [uk3.php.net] and fwrite [uk3.php.net] functions.


$log = "$link - $time - $ip\n"; // or whatever format you want to use.
$fh = fopen('file/name', 'ab');
fwrite($fh, $log);
fclose($fh);

Habtom

9:06 am on Nov 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just before the redirection:

$log_data = $myip."/t".$otherinfo;
$fi_log = fopen($file_log, 'w') or die("can't open file");
fwrite($fi_log, $log_data);
fclose($fi_log);

Habtom

9:26 am on Nov 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PHP_Chimp, basically the same thing I posted, didn't notice your post.

PHP_Chimp

10:06 am on Nov 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No problem.
Although if you open the file in 'w' mode then you will always be overwriting any information in that file, so the log will only ever contain the last request made. With 'a' they can add information to the end of that file then examine that file once a week (or whenever) to see you they have been linking to.

However my example doesnt have any error handling, so they need a bit of both ;)

smallcompany

6:29 pm on Nov 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks a lot.

We'll see how that works.