Gossamer Forum
Home : Products : Links 2.0 : Customization :

Jump with log extion

Quote Reply
Jump with log extion
Hi Eliot. I've made this modification and it seems to work correctly... This is my modified jump.pl:

#!/usr/local/bin/perl
# -------------
# Links
# -------------
# Links Manager
#
# File: jump.pl
# Description: Increments the number of hits for the specified link,
# and sends the user off to the appropriate page.
# Author: Alex Krohn
# Email: alex@gossamer-threads.com
# Web: http://www.gossamer-threads.com/
# Version: 2.0
#
# (c) 1998 Gossamer Threads Inc.
#
# This script is not freeware! Please read the README for full details
# on registration and terms of use.
# =====================================================================
#
# Form Input:
# '$db_key' = key number # Send as form input the key name and key value
# # of the link you want to go to.
#
# Setup:
# Make sure the require statement below points to the config file.

# Required Librariers
# --------------------------------------------------------
eval {
($0 =~ m,(.*)/[^/]+,) && unshift (@INC, "$1"); # Get the script location: UNIX /
($0 =~ m,(.*)\\[^\\]+,) && unshift (@INC, "$1"); # Get the script location: Windows \

require "admin/links.cfg"; # Change this to full path to links.cfg if you have problems.
require "$db_lib_path/db_utils.pl";
require "$db_lib_path/links.def";
};
if ($@) {
print "Content-type: text/plain\n\n";
print "Error including libraries: $@\n";
print "Make sure they exist, permissions are set properly, and paths are set correctly.";
}

# ========================================================

eval { &main; }; # Trap any fatal errors so the program hopefully
if ($@) { &cgierr("fatal error: $@"); } # never produces that nasty 500 server error page.
exit; # There are only two exit calls in the script, here and in in &cgierr.

sub main {
# --------------------------------------------------------

my %in = &parse_form();
my ($goto, $id, $delim, $time, $ZIPURL);

$id = $in{$db_key};
$delim = quotemeta($db_delim);
$time = time();
my (%rec) = &get_record ($id);

if ($id eq "random") {
my ($count, $rand, $find);

# Pull out the total number of links.
open (COUNT, "<$db_hits_path/index.count") or &error ("unable to open index count file: $db_hits_path/index.count. Reason: $!");
$count = int <COUNT>;
close COUNT;

# Get the random line from the url lookup database.
srand;
$find = 0; $rand = int (rand ($count + 0.5)); ($rand == $count) and ($rand--);
open (URL, "<$db_url_name") or &error ("unable to open url database: $db_url_name. Reason: $!");
while (<URL>) {
$find++ == $rand or next;
/\d+$delim(.+)/o or next;
$goto = $1;
last;
}
close URL;
$goto or &error ("Can't find random line: $rand.");
}
elsif (exists $in{$db_key}) {
# Make sure this is a valid looking id.
($id =~ /^\d+$/) or &error ("Invalid id: $id");

# Let's get the URL.
open (URL, "<$db_url_name") or &error ("unable to open url database: $db_url_name. Reason: $!");
while (<URL>) {
(/^$id$delim(.+)/o) or next;
chomp ($goto = $1);
last;
}
close URL;
$goto or &error ("Can't find link id: $id");

# Bump the counter one.
if (open (HIT, "<$db_hits_path/$id")) {
my ($count, $old_time, @IP, $ip, $visited);
chomp ($count = <HIT>);
chomp ($old_time = <HIT>);
chomp (@IP = <HIT>);
(($time - $old_time) > 21600) and (@IP = ());
foreach $ip (@IP) {
$ip eq $ENV{'REMOTE_ADDR'} and ($visited++ and last);
}
if (!$visited) {
push (@IP, $ENV{'REMOTE_ADDR'});
$count = $count + 1;
open (HIT, ">$db_hits_path/$id") or &error ("Can't open for output counter file. Reason: $!");
if ($db_use_flock) { flock (HIT, 2) or &error ("Can't get file lock. Reason: $!"); }
local $" = "\n";
print HIT "$count\n$time\n@IP";
close HIT;
}
}
else {
open (HIT, ">$db_hits_path/$id") or &error ("Can't increment counter file. Reason: $!");
print HIT "1\n$time\n$ENV{'REMOTE_ADDR'}";
close HIT;
my $curr_time = time;
$url = $rec{'URL'};

if (-e $db_jump_log) {
$file_size = (-s "db_jump_log");
if ($file_size > 1000000) {
open (LOG,">$db_jump_log") or &cgierr ("Error [search.pl]: Unable to create search log file. Reason: $!");
flock (LOG, 2) or &cgierr ("Error [search.pl]: Unable to flock search log file. Reason: $!");
print LOG "$url|1|$curr_time\n";
close (LOG);
}
else {
open (LOG, "<$db_jump_log") or &cgierr ("Error [search.pl]: Unable to open search log file. Reason: $!");
@kwords = <LOG>;
close(LOG);

$newfile = "";
$found = 0;
foreach $line (@kwords) {
chomp $line;
($word, $count, $oldtime) = split (/\|/,$line);
if ($url eq lc($word)) {
$count++;
$found++;
$newline = "$word|$count|$curr_time\n";
$newfile .= $newline; }
else { $newfile .= "$line\n"; }
}
if (! $found) {
$newline = "$url|1|$curr_time\n";
$newfile .= $newline;
}
open (LOG,">$db_jump_log")or &cgierr ("Error [search.pl]: Unable to open search log file. Reason: $!");
flock (LOG, 2) or &cgierr ("Error [search.pl]: Unable to open search log file. Reason: $!");
print LOG $newfile;
close (LOG);
}
}
else {
open (LOG, ">$db_jump_log") or &cgierr ("Error [search.pl]: Unable to create search log file. Reason: $!");
flock (LOG, 2) or &cgierr ("Error [search.pl]: Unable to flock search log file. Reason: $!");
print LOG "$url|1|$curr_time\n";
close (LOG);
chmod (0666, "$db_jump_log") or &cgierr ("Error [search.pl]: Cannot chmod search log file. Reason: $!");
}
}

# Now let's send the user to the url..
$goto ?
print "Location: $goto\n\n" :
&error ("Record not found ($in{$db_key})");
}
}

sub error {
# ------------------------------------------
#
print "Content-type: text/plain\n\n";
print "Error: $_[0]\n";
exit;
}
Quote Reply
Re: Jump with log extion In reply to
Why are you using a separate sub to log more information? I don't understand what you are attempting to do.

BTW: I have added codes in this forum before for adding a separate log file to the rate.cgi file that can be adapted to the jump.cgi script.

Regards,

------------------
Eliot Lee....
Former Handle: Eliot
Anthro TECH, L.L.C
anthrotech.com
* Check Resource Center
* Search Forums
* Thinking out of the box (codes) is not only fun, but effective.


Quote Reply
Re: Jump with log extion In reply to
Hi Eliot, I need this log option to see which links are choosen by users and how many times this has happend. I working on a script that read all those different logs and displays the with grafics...

------------------
Quote Reply
Re: Jump with log extion In reply to
You need to learn more about creating and calling subs within .cgi/.pl scripts.

First of all, log_search; is incorrect code for calling the sub in another sub.

Secondly, you need to place the following code:

Code:
&log_search;

after the following set of codes:

Code:
# Now let's send the user to the url..
$goto ?
print "Location: $goto\n\n" :
&error ("Record not found ($in{$db_key})");

Hope this helps.

Regards,

------------------
Eliot Lee....
Former Handle: Eliot
Anthro TECH, L.L.C
anthrotech.com
* Check Resource Center
* Search Forums
* Thinking out of the box (codes) is not only fun, but effective.


Quote Reply
Re: Jump with log extion In reply to
Hi Eliot thanx for the quick responding. I've modified the first posting and put the code into it where you told me...

but the result in my keyword2.txt logfile is wrong. It save the following code:

cgi-bin/admin/data/url.db|2|958259671

this must be like:
http://www.micrtosoft.com|2|958259671

How to change this???
Thanx



------------------
Quote Reply
Re: Jump with log extion In reply to
The following codes are wrong:

Code:
if ($term eq lc($word)) {

$term is not the correct variable to use.

Change the following codes:

Code:
$newfile = "";
$found = 0;
foreach $line (@kwords) {
chomp $line;
($word, $count, $oldtime) = split (/\|/,$line);
if ($term eq lc($word)) {
$count++;
$found++;
$newline = "$word|$count|$curr_time\n";
$newfile .= $newline;
}

to the following codes:

Code:
$newfile = "";
$word = $rec{'URL'};
$found = 0;
foreach $line (@kwords) {
chomp $line;
($word, $count, $oldtime) = split (/\|/,$line);
$count++;
$found++;
$newline = "$word|$count|$curr_time\n";
$newfile .= $newline;

-OR-

Try changing the following codes:

Code:
my $term = lc($db_url_name);

to the following codes:

Code:
my $term = $rec{'URL'};

And if this doesn't work...I would highly suggest using the following codes:

Code:
(-e $db_jump_log) ?
open (LOG,">>$db_jump_log") :
open (LOG,">$db_jump_log");
if ($db_use_flock) {
flock (LOG, 2) or &cgierr ("Can't get file lock. Reason: $!");
}
my $curr_time = time;
$url = $rec{'URL'};
$count++;
$logline="$url\|$count\|$curr_time\n";
print LOG $logline;
close(LOG);

These codes should go AFTER the following codes:

Code:
print HIT "$count\n$time\n@IP";
close HIT;

Then define the $db_jump_log variable in the links.cfg file similar to how you defined the $db_file_name variable.

These similar codes have been posted in this forum before (twice).

Regards,

------------------
Eliot Lee....
Former Handle: Eliot
Anthro TECH, L.L.C
anthrotech.com
* Check Resource Center
* Search Forums
* Thinking out of the box (codes) is not only fun, but effective.




[This message has been edited by AnthroRules (edited May 13, 2000).]
Quote Reply
Re: Jump with log extion In reply to
Hi Eliot, In the first posting I've reposted my jump.pl again.

I've put:
$db_jump_log = 'C:/inetpub/wwwroot/pages/keywords2.txt';

into links.cfg and I get this error:

Building Pages
Step: Updating New and Popular Records and rebuilding URL database

Pages built on 14-May-2000 at 03:01:16
--------------------------------------------------------

Backing up database . . .
Backup exists for today.. Skipping
Done (0 s)

Building URL Index . . .
Done (0 s)

Updating New and Popular Records . . .
Warning: No date for line: |1|958264264| &#0124; &#0124; &#0124; &#0124; &#0124; &#0124;No|No| &#0124; &#0124; &#0124; &#0124; &#0124; &#0124; &#0124; &#0124;. Skipping..
Warning: No date for line: |1|958264472| &#0124; &#0124; &#0124; &#0124; &#0124; &#0124;No|No| &#0124; &#0124; &#0124; &#0124; &#0124; &#0124; &#0124; &#0124;. Skipping..
Warning: No date for line: |1|958264702| &#0124; &#0124; &#0124; &#0124; &#0124; &#0124;No|No| &#0124; &#0124; &#0124; &#0124; &#0124; &#0124; &#0124; &#0124;. Skipping..

and in my keyword2.txt logfile I have only:
|1|958266520


[This message has been edited by Hacktor (edited May 13, 2000).]
Quote Reply
Re: Jump with log extion In reply to
First of all, with the codes I provided, you do not need to re-build your index, since the log file is dynamically created. What have you done with the nph-build.cgi file?

This code needs to be placed in your links.cfg file:

Code:
$db_jump_log = 'C:/inetpub/wwwroot/pages/keywords2.txt';

And actually, these codes should be the following:

Code:
$db_jump_log = "C:/inetpub/wwwroot/pages/keywords2.txt";

Didn't I state that the variable needs to be configured exactly like the $db_file_name???

Then add the following codes:

Code:
my (%rec) = &get_record ($id);

AFTER the following codes:

Code:
$time = time();

This should make the $url variable work.

Regards,

------------------
Eliot Lee....
Former Handle: Eliot
Anthro TECH, L.L.C
anthrotech.com
* Check Resource Center
* Search Forums
* Thinking out of the box (codes) is not only fun, but effective.


Quote Reply
Re: Jump with log extion In reply to
YOU DID NOT FOLLOW MY LAST SUGGESTION!!

D E L E T E the $count++; line!!!!!!!!!!

Regards,

------------------
Eliot Lee....
Former Handle: Eliot
Anthro TECH, L.L.C
anthrotech.com
* Check Resource Center
* Search Forums
* Thinking out of the box (codes) is not only fun, but effective.


Quote Reply
Re: Jump with log extion In reply to
Thanx Eliot for your patience (will be hard) and your help.
The script works but I doesn't count the times and URL has visit....

This is my keywords2.txt:
http://go.to/hacktor|1|958293861
http://www.rolf01.com|1|958293918
ftp://www.sample45.com|1|958293942

I've visit http://go.to/hacktor a few times but it count only one time....

In the first posting is the jump.pl, I have now at the moment....

Thanx Eliot


------------------
Quote Reply
Re: Jump with log extion In reply to
Remove the following code:

Code:
$count++;

Regards,

------------------
Eliot Lee....
Former Handle: Eliot
Anthro TECH, L.L.C
anthrotech.com
* Check Resource Center
* Search Forums
* Thinking out of the box (codes) is not only fun, but effective.


Quote Reply
Re: Jump with log extion In reply to
Dear Eliot, in the first posting is my jump.pl which I use now.

It writes coreectly into the logfile but the counter doesn't work correctly.
This is my logfile:
http://go.to/hacktor|1|958305341
http://www.sample44.com|1|958305372

I've visit http://go.to/hacktor a few times but the counter stays at |1|
can you tell me whats wrong in the script in the first posting....
Thanx Eliot....)


[This message has been edited by Hacktor (edited May 14, 2000).]
Quote Reply
Re: Jump with log extion In reply to
Dear Eliot, sorry but I've posted this message again, because I couldn't find this posting. Will try to remove it...
Okay everything works fine now but I made a mistake save the correct info into my logfile.

This script writes the info now:
http://www.microsoft.com|1|958511498
ftp://go.to/hacktor|1|958511466
http://altavista.com|1|958511466


but this must be:
www.microsoft.com|1|958511498
go.to/hacktor|1|958511466
altavista.com|1|958511466

http:// and/or ftp:// has to be removed. Can you help me how to do this... Thanx Eliot...



Quote Reply
Re: Jump with log extion In reply to
In Reply To:
Can you help me how to do this.
May be later...

One hint to get you started....

Create another url.db file that removes the protocols (http:// and ftp:// and news://). Look in the nph-build.cgi file.

Another suggestion is to search this forum for information on regular expressions and conditional statements that you can use in the jump.cgi file to "delete" the protocols from the URLs when writing to the log file.

Good luck.

Regards,

Eliot Lee
Anthro TECH, L.L.C
Web: http://www.anthrotech.com/
Quote Reply
Re: Jump with log extion In reply to
it's for using a script to read these logfiles and displays them to the visitors. It's displays them grapical
Example:
www.microsoft.com - 80 visits : xxxxxxxxxxxxxxx
www.astalavista.com - 20 visits : xxxxx
etc...
But I'm a beginner and I work hard to learn and understand Perl...

Quote Reply
Re: Jump with log extion In reply to
And did you even use my suggestions???

Regards,

Eliot Lee
Anthro TECH, L.L.C
Web: http://www.anthrotech.com/
Quote Reply
Re: Jump with log extion In reply to
Yes of course, I read a few postings to see what the code is and how to put it into my script. But I get some errors and I fu...up my script... (have a backup).... Have also a Perl book. I could need some little help...

Quote Reply
Re: Jump with log extion In reply to
In Reply To:
But I get some errors and I fu...up my script..(have a backup).... Have also a Perl book. I could need some little help...
W H A T ERRRORS!>!>!>!>!?!?!?!?!?!?!?!?!?!??!?!?!??!?!?!?!?!??!?!

The preceding statement you posted does NOTHING to help me to help you! PLEASE post the exact error messages you receive...both syntax related and CGI related. Do NOT post Internal Server Error...my reaction will be so what?.

WinkFrown

Regards,

Eliot Lee
Anthro TECH, L.L.C
Web: http://www.anthrotech.com/
Quote Reply
Re: Jump with log extion In reply to
Hi Eliot, I read some postings and I tried with cut/past to put some codes in my jump.pl (see the first posting). Which codes? I don't know, I tried a lot of it. Because I don't know which codes it must be and where to put it. So I tried, tried and tried again, but it doesn't work

You said:
Another suggestion is to search this forum for information on regular expressions and conditional statements that you can use in the jump.cgi file to "delete" the protocols from the URLs when writing to the log file.

Can you and will you help me with this code. See the first posting for my script.

This script writes the info now:
http://www.microsoft.com|1|958511498
ftp://go.to/hacktor|1|958511466
http://altavista.com|1|958511466


but this must be:
www.microsoft.com|1|958511498
go.to/hacktor|1|958511466
altavista.com|1|958511466

http:// and/or ftp:// has to be removed.

Thanx Eliot....

Quote Reply
Re: Jump with log extion In reply to
Hi Eliot, can or will you help me with this one... I know you're very busy...

Thanks alot...

Quote Reply
Re: Jump with log extion In reply to
Try the following codes:

Code:

$url =~s/http:\/\///g;
$url =~s/news:\/\///g;
$url =~s/mailto:\/\///g;


AFTER the following codes:

Code:

$url = $rec{'URL'};


I don't gaurantee these codes will work...but they may shed some light.

Sorry for not responding earlier.

Smile

Hope this helps.

Regards,

Eliot Lee
Quote Reply
Re: Jump with log extion In reply to
Thanx Eliot for your help again. I've made some modifications (posted my jump.pl again in the first posting) and the log options seems to work correctly.
There is only 1 error in it. I've a problem with the random link. This is the error message:
CGI Error
The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are:

I cann't find it we there problem is in...

(After solving this problem the script is ready to use)

Quote Reply
Re: Jump with log extion In reply to
I can't really tell the problem without seeing a current copy of your jump.cgi file. I believe you have an eariler version posted in your first Posting.

Either Replace the codes you've listed with the newer codes -OR- better yet, save your jump.cgi file as a .txt file and then upload the text file to a publicly accessible directory in your web server, and then post the URL to the text file in this Thread.

Regards,

Eliot Lee
Quote Reply
Re: Jump with log extion In reply to
Thanks Eliot for responding. I've modified the first posting with the jump.pl which I use now...

Quote Reply
Re: Jump with log extion In reply to
I belive that the problem is with the $count variable that is used in the random section of your jump.pl file. I believe that this $count variable is being confused with the latter $count variable.

I would recommend changing the following codes:

Code:

if ($id eq "random") {
my ($count, $rand, $find);

# Pull out the total number of links.
open (COUNT, "<$db_hits_path/index.count") or &error ("unable to open index count file: $db_hits_path/index.count. Reason: $!");
$count = int <COUNT>;
close COUNT;

# Get the random line from the url lookup database.
srand;
$find = 0; $rand = int (rand ($count + 0.5)); ($rand == $count) and ($rand--);
open (URL, "<$db_url_name") or &error ("unable to open url database: $db_url_name. Reason: $!");
while (<URL>) {
$find++ == $rand or next;
/\d+$delim(.+)/o or next;
$goto = $1;
last;
}


with the following codes:

Code:

if ($id eq "random") {
my ($cnt, $rand, $find);

# Pull out the total number of links.
open (COUNT, "<$db_hits_path/index.count") or &error ("unable to open index count file: $db_hits_path/index.count. Reason: $!");
$cnt = int <COUNT>;
close COUNT;

# Get the random line from the url lookup database.
srand;
$find = 0; $rand = int (rand ($cnt + 0.5)); ($rand == $cnt) and ($rand--);
open (URL, "<$db_url_name") or &error ("unable to open url database: $db_url_name. Reason: $!");
while (<URL>) {
$find++ == $rand or next;
/\d+$delim(.+)/o or next;
$goto = $1;
last;
}


Notice that I simply replaced $count with $cnt.

Hope this helps. If it doesn't, I don't know what to tell you!

Good luck!

Regards,

Eliot Lee