View Full Version : Difficult to explain.. help
Richard
01-06-2003, 09:32 PM
What I want to do is use PHP to log refering websites to one file and have the amount of times someone has been refered from that site next to the site url and also increase that number by one each time a user is refered from it again.
The txt log file will look somthing like:
http://www.google.com,5
http://www.lycos.co.uk,9
And so on. The problem is I don't know what functions etc to use in this script. First I want to get everything in the $HTTP_REFERER before the 3rd forward slash, so only the root address is logged. Then need to check if the url entry already exists, and if so increase the number after the comma by one. If it doesn't exist, create a new line with the url comma and 1.
I'm not good at explaining these things so.. :) But if you can help me out I would be grateful :)
Spookster
01-06-2003, 11:08 PM
Well you could just parse your server's access log for your site and get that information.
As for doing it the other way I wouldn't store it in a txt file as it get large quite fast and having to search through that file often would bog your server and your site down. You really a database for something like that.
Richard
01-07-2003, 03:49 PM
Spookster.. It wouldn't get large because only about a dozen urls ill be in it. This is why I asked if anyone knew how to get the root host of a $http_referer, so not the whole url and query string etc are logged, only the root. I found out how to do that anyway using parse_url (host).
I need to find out how to do the rest now.. :confused:
Akito
01-07-2003, 07:38 PM
Its actually easier to get a program like Analog to look at the stats, like spookster was saying. Unless you need to do it the other way, in which case I don't know.
Richard
01-07-2003, 11:03 PM
Akito, I'm not allowed to use Analog or any other program like that on my webspace, and I can't be bothered downloading logs all the time and processing them myself.
It's not just referers that I want to use this on, so if someone can actually answer the original question (:rolleyes: ) on what functions etc to use then I'd be grateful.
firepages
01-08-2003, 03:18 AM
I just did one the other week... its a class cos it was written for GTK app & a bit long-winded but works non-the-less ;)
note that it does not check if the delimiter (here a comma) is duplicated in the string so be careful (eg $log->set('my its, a comma',','); will do something horrible!)
eg
<?
include('logfile.class');
$referer='http://www.localhost/';
/*set the logfile (creates if not exists) & the expected seperator*/
$logs=new logfile('logfile.txt',',');
/*increment url count (or create entry if not exists)*/
$logs->set($referer);
//get a value ? ... echo $logs->get('http://localhost');
?>
<?
class logfile{
function logfile($logfile,$seperator){
$this->logfile=$logfile;
$this->seperator=$seperator;
$this->_init();
}
function _init(){
if(!is_file($this->logfile)){//creat file if no exists
touch($this->logfile)or Die($this->logfile.' is not writable - check permissions');
}
$logs=file($this->logfile);
for($x=0;$x<count($logs);$x++){
list($key,$count)=split($this->seperator,trim($logs[$x]));
$array[$key]=$count;
}
$this->array=$array;
}
function set($string){
$array=$this->array;
if(is_array($array) && array_key_exists($string,$array)){
++$array[$string];
}else{
$array[$string]=1;
}
$this->array=$array;
$this->_save();
}
function get($string){
$array=$this->array;
if(array_key_exists($string,$array)){
return $array[$string];
}else{
return $string ." not foumd in ".$this->logfile;
}
}
function _save(){
$array=$this->array;
if(is_writable($this->logfile)){
$fp=fopen($this->logfile,'w');
while(list($key,$var)=each($array)){
fputs($fp,"$key,$var\n");
}
fclose($fp);
}else{
Die($logfile.' is not writable - check permissions');
}
}
}
?>
Richard
01-08-2003, 03:21 PM
Firepages, the only problem is that my logs are stored in directories, and the names of those directories go by the month.
So
$logs=new logfile('/home/domains/example.com/user/htdocs/logs/$month/refer.txt',',');
doesn't work, because the class file script thinks the directory is actually called '$month', not 'Jan2003'.
LaundroMat
01-08-2003, 03:23 PM
Shouldn't you concat the month string?
$logs=new_logfile('/home/domains/example.com/user/htdocs/logs/' . $month . '/refer.txt',',');
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.