Web Hosting Talk







View Full Version : PHP/Click-through logger.


SepsisNet
05-03-2001, 04:08 AM
I would like input on PHP click-thru loggers. Most out there are done in Perl, but I'm interesting in experimenting with PHP.

A link would look something like http://mydomain.com/redirect.php?destination=http://www.outgoingtraffic.com/

(SH)Saeed
05-03-2001, 05:59 AM
I made one for my website in PHP. Do you want it to save the data in flat files or in mysql?

SepsisNet
05-03-2001, 06:05 AM
I can go either way. If youdon't mind, may I have copy?

(SH)Saeed
05-03-2001, 06:30 AM
Sure, just create an empty file and add the code below to it and then save it as lets say ""redirect.php"".

<?php
mysql_connect("localhost", "username", "password");
mysql_select_db("database_name");
$date = date("Y-m-d H:i:s");
$ip = getenv("REMOTE_ADDR");
$result = mysql_query("INSERT INTO links(date, ip, url) VALUES('$date', '$ip', '$url')");
mysql_close();
header("Location: $url");
?>

Now change all your links to "redirect.php?url=http://www.domain.com/directory/". The script will take this code and put it in the database you set it to. Make sure you have a table that has the columns "date", "ip" and "url". You can also add "id" column at first and set it to auto_increment.. You can later use php/mysql to use the info however you wish.

That's about it..enjoy..
:cool:

Racin' Rob
05-03-2001, 04:28 PM
Thanks very much, it works great!

(SH)Saeed
05-03-2001, 04:56 PM
You're welcome. :)

(SH)Saeed
06-17-2001, 08:30 PM
Since a few people have emailed me and asked me how to do if they want the script to output the information into a text file, I have coded a new script that has both options.

<?php

//NOTES
//------------------------------------------------------------

//Coded by Sarvi Technologies, June 2001.

//How does this work?
//You simply link to redirect.php?url=http://www.domain.com/

//Incase of permission error when writing out to a text file,
//manually create the log file and chmod it to 777.

//The MySQL database should have the fields "date", "ip" and
//"url".

//CONFIGURATION
//------------------------------------------------------------

//Output type (1 = MySQL, 2 = Text File)
$outPut = "2";

//MySQL
$host = "localhost";
$user = "username";
$pass = "password";
$db = "database";

//Text File
$file = "redirect.log";

// DO NOT EDIT BELOW HERE
//------------------------------------------------------------

$date = date("Y-m-d H:i:s");
$ip = getenv("REMOTE_ADDR");

if($outPut == "1") {
mysql_connect($host, $user, $password);
mysql_select_db($db);
$result = mysql_query("INSERT INTO links(date, ip, url) VALUES('$date', '$ip', '$url')");
mysql_close();
} else if($outPut == "2") {
$out = fopen($file, "a");
fputs($out, "$date\t$ip\t$url\n");
fclose($out);
chmod($file, 0777);
} else {
header("text/html");
echo "Wrong output!";
exit;
}

header("Location: $url");

?>


If someone could test both parts of this script and let me know how they work, I would appriciate it. At the moment I am very busy and do not have time to set up database to test run the script.

Enjoy.

Newbuyer
06-17-2001, 09:51 PM
Cool - this is great! A nice simple script. If anyone's wondering, I've extracted just the text logging portion of this script as follows:

<?php
$file = "logs/link.log";
$date = date("Y-m-d H:i:s");
$ip = getenv("REMOTE_ADDR");
$out = fopen($file, "a");
fputs($out, "$date\t$ip\t$url\n");
fclose($out);
header("Location: $url");
?>

Works perfectly. I 666 CHMODed link.log beforehand. Thanks Zolbian!

(SH)Saeed
06-18-2001, 01:27 AM
You're welcome. Just don't forget to start URL's with http://, otherwise the browser will not know that it's a new URL and will add it to the existing URL.

Saeed

wmac
06-21-2001, 09:09 PM
Hello

I have written a perl copy for my own site.

This is the code:

#!/usr/local/bin/perl
#
# Author name: Siamak Sarmady
# Creation date: Jan 2001
#
# Description: Log and Redirector
# Site: www.onlineprogrammer.org

$temp=$ENV{'QUERY_STRING'};
@pairs=split(/&/,$temp);
foreach $item(@pairs) {
($key,$content)=split (/=/,$item,2);
$content=~tr/+/ /;
$content=~ s/%(..)/pack("c",hex($1))/ge;
$fields{$key}=$content;
}

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
if (length ($min) == 1) {$min = '0'.$min;}
$date="$mon/$mday/$year, $hour:$min";

open (gbfile, ">> redirlog.txt");

print $fields{'url'};
print gbfile $date;
print gbfile "\n";
close (gbfile);

print "Location: $fields{'url'}\n\n";


Regrads
Mac
:)