Gossamer Forum
Home : General : Perl Programming :

Only allow webpage if visited from ...

Quote Reply
Only allow webpage if visited from ...
Hi there everyone.
I have seen these scripts around but can't remember exactly where.

My problem is that I am using links 2.0 script and I have detailed pages being built. I get around about 200 - 300 visitors a day nothing to large, but my hits out average about 8 to 9,000 a day without fail.

I believe that some other webmasters out there are using my links on their web sites. Is their a script that would stop people from doing this and how would I link this into links 2.0 scripting. I guess the mod would have to be done in the jump.cgi but unsure on the correct syntax.

Can anyone help.
Quote Reply
Re: [stu2000] Only allow webpage if visited from ... In reply to
A hit is a different from a visitor. If you load a webpage with 5 images on it, it will register as 5 hits if I recall correctly... therefore it is very likely that the 300 visitors are registerting 9,000 hits... I'm not too sure on this, but that would be my guess.
Cheers,
Michael Bray
Quote Reply
Re: [Michael_Bray] Only allow webpage if visited from ... In reply to
Hi there thanx for your reply. I think I worded my statement above wrongly. My download links amy jump<ID> tags are getting 9,000 hits a day. I only have about 100 links on my directory and I find it hard to believe that out of 300 visitors a day. Each visitor would download this many links.

I am pretty sure they are being leeched, I can't unfortunately prove this thou. I have thought of implementing the anti-leech script but this invloves changing all my urls.

Basically I require a script like. if reffered from my site allow download -- if reffered from external page goto a sorry html page.

I have seen similar scripts around just unsure if it can be used in links 2.0 and if it can where is the mod needed.
Quote Reply
Re: [stu2000] Only allow webpage if visited from ... In reply to
Add something into jump.cgi like:

Code:
foreach my $referer (@db_referers) {
unless ($ENV{HTTP_REFERER} =~ /^\Q$referer\E/) {
barf
last;
}
}

But it isn't fool proof as referers can be faked but it is better than nothing.

You'll notice that @db_referers is already in links.cfg

Last edited by:

Paul: Aug 31, 2002, 6:58 AM
Quote Reply
Re: [Paul] Only allow webpage if visited from ... In reply to
Hi Paul, thanx for the reply.

However, How would I modify the above code to send them to a page if they visit from an external link.

For example if a link of mine was on a competitors web site and a visitor clicked on it. Instead of them viewing the link I would like them redirected to my homepage.

But if the visitor came from my site allow download.

One last point is the web address for @db_referers an absolute path or an http://www entry
Quote Reply
Re: [stu2000] Only allow webpage if visited from ... In reply to
Code:
foreach my $referer (@db_referers) {
unless ($ENV{HTTP_REFERER} =~ /^\Q$referer\E/) {
print "Location: $build_root_url \n\n";
last;
return;
}
}

The format is a url like:

http://www.domain.com
http://domain.com
Quote Reply
Re: [Paul] Only allow webpage if visited from ... In reply to
Hi again Paul, Sorry about this but I am getting 500 errors, unfortunately I do not have access to my logs.

It happens when I click on my link on detailed pages.

I added the following codes

Links.cfg

# Referers -- which hosts are allowed to add to your database.
@db_referers = ('http://www.mysite.info/','http://mysite.info','mysite.info','www.mysite.info');

and then in jump.cgi I added

# Make sure this is a valid looking id.
($id =~ /^\d+$/) or &error ("Invalid id: $id");

########## Paul W Anti-leech script #############################
# Check to see if visitor arrived at website correctly
# http://www.gossamer-threads.com/perl/gforum/gforum.cgi?post=214216

my $referer (@db_referers) {
unless ($ENV{HTTP_REFERER} =~ /^\Q$referer\E/) {
print "Location: $build_root_url \n\n";
last;
return;
}
}
####### End of Pauls Mod #########

# 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");

As I said before it just gives me 500 error messages.
Quote Reply
Re: [stu2000] Only allow webpage if visited from ... In reply to
Also am I right in saying, if I wanted to use this script in another cgi file I could use something like:-


--------------------------------------------------------------------------------
Code
--------------------------------------------------------------------------------
foreach my $referer (@db_referers) {
unless ($ENV{HTTP_REFERER} =~ /^\Q$referer\E/) {
print "Location: $build_root_url \n\n";
last;
return;
}
}

--------------------------------------------------------------------------------

But intstead of (@db_referers) use (URL) and then insert a piece of code above with all my url's in. e.g

my URL = ('http://www.mysite.info/','http://mysite.info','mysite.info','www.mysite.info');

foreach my $referer (@URL) {
unless ($ENV{HTTP_REFERER} =~ /^\Q$referer\E/) {
print "Location: $build_root_url \n\n";
last;
return;
}
}


As you know my Perl is very poor, I am learning but at the rate of one step forward and 5 steps back a day.Crazy
Quote Reply
Re: [stu2000] Only allow webpage if visited from ... In reply to
Code:
########## Paul W Anti-leech script #############################
# Check to see if visitor arrived at website correctly
# http://www.gossamer-threads.com/perl/gforum/gforum.cgi?post=214216

foreach my $referer (@db_referers) {
unless ($ENV{HTTP_REFERER} =~ /^\Q$referer\E/) {
print "Location: $build_root_url \n\n";
last;
return;
}
}
####### End of Pauls Mod #########

You didn't copy and paste properly :)
Quote Reply
Re: [Paul] Only allow webpage if visited from ... In reply to
Hi Paul, thanx for pointing out that silly mistake of mine.

I tried everything too. Sorry.

Ok the problem I am having now is that when I come from my website, the details.cgi I am always sent to my homepage.

Here is what I have in links.cfg

@db_referers = ('http://www.mywebsite.info/cgi-bin/links/detail.cgi','http://www.mywebsite.info/','http://mywebsite.info','mywebsite.info');

I have blanked out my url as my directory is not protected yet.

Any Ideas
Quote Reply
Re: [stu2000] Only allow webpage if visited from ... In reply to
Just above the foreach loop add:

&cgierr($ENV{HTTP_REFERER});

....and see what it shows after coming from detail.cgi
Quote Reply
Re: [Paul] Only allow webpage if visited from ... In reply to
Hi Paul did that and it produces an error message, see attached jump.txt file.

It doesn't really tell me anything.

I have also attatched my jump.cgi file.

Everything else above remains unchanged eg links cfg...

Also whilst I am here are you selling your stats.cgi mod as advertised about a year back

Last edited by:

stu2000: Sep 1, 2002, 5:17 AM
Quote Reply
Re: [stu2000] Only allow webpage if visited from ... In reply to
Yeah the error is correct, that what I wanted it to do...the referer was:

http://www........info/cgi-bin/links/detail.cgi?ID=21

.....so really it should be letting you download as in your referer list you have:

http://www.mywebsite.info/cgi-bin/links/detail.cgi

The regex is:

^\Q$referer\E

...which is saying match any referer that begins with:

http://www.mywebsite.info/cgi-bin/links/detail.cgi

stats.cgi disappeared ages ago, sorry :( .....I think yogi has a copy though.
Quote Reply
Re: [Paul] Only allow webpage if visited from ... In reply to
I don't have a copy, no.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Only allow webpage if visited from ... In reply to
Ok, so in a way the script works and it doesn't.

The problem in which it seems is in the links.cfg file.

I tried to add some thing like this:-
http://www.............info/cgi-bin/links/detail.cgi?ID={$rec(ID)}

But as you may of guessed it still does not work.

Last edited by:

stu2000: Sep 1, 2002, 5:43 AM
Quote Reply
Re: [stu2000] Only allow webpage if visited from ... In reply to
Got it sorted found this code.

# Check the referer.
if (@db_referers and $ENV{'HTTP_REFERER'}) {
$found = 0;
foreach (@db_referers) {
$ENV{'HTTP_REFERER'} =~ /$_/i and $found++ and last;
}
if (!$found) {
print "Content-type: text/html\n\n";
print qq|<center> <font size="5" color="#FF0000"><b><u>NOTICE</u>:</b></font> <p>
<b>You are attempting to execute this script from an unauthorized domain. <br>This script may only be executed from the '<font color="#FF0000">orphanage.com</font>' domain.</b> </center>|; exit;
}
}

from http://www.gossamer-threads.com/...BHTTP_REFERER;#29163
Quote Reply
Re: [stu2000] Only allow webpage if visited from ... In reply to
Thats practically identical to mine but glad you got it working anyway.
Quote Reply
Re: [Paul] Only allow webpage if visited from ... In reply to
Well it doesn't work. I thought it did, but when I link to it externally I get the link anyway.

I have posted a question to Bobsie in the links 2.0 customation threads to see if he has any input.
Quote Reply
Re: [stu2000] Only allow webpage if visited from ... In reply to
A solution for this problem was finally produced by Eliot and Paul, for the full code goto http://www.gossamer-threads.com/...view&post=214546
Quote Reply
Re: [stu2000] Only allow webpage if visited from ... In reply to
Just as I turned off my pc last night I realised the lame mistake I made...no-one else picked up on it either......change the code to:

Code:
my $flag = 0;

foreach my $referer (@db_referers) {
if ($ENV{HTTP_REFERER} =~ /^\Q$referer\E/i) {
$flag++;
last;
}
}

$flag or print "Location: $build_root_url \n\n";

Last edited by:

Paul: Sep 2, 2002, 2:18 AM