CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Removing /document.extension from HTTP_REFERER

 



abockover
Deleted

Sep 26, 2000, 10:58 PM

Post #1 of 2 (557 views)
Removing /document.extension from HTTP_REFERER Can't Post

When someone clicks a link on a page that links to http://myserver.com/cgi-bin/ref.pl, the script will get the server name of the refering website using $ENV{'HTTP_REFERER'}. If I have the script print this out it looks like: HTTP_REFERER: http://referingsite.com/referingdocument.ext

How can remove the /referingdocument.ext from http://referingsite.com (I think it can be done using regular expressions, but I am not sure)?

This way I can use
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


if($servername eq "whatever.com") {
#do something
} else {
#do something else
}
</pre><HR></BLOCKQUOTE>
to see how many people are coming from where.

Could someone help?

Thanks,

Aaron


japhy
Enthusiast

Sep 27, 2000, 11:58 AM

Post #2 of 2 (557 views)
Re: Removing /document.extension from HTTP_REFERER [In reply to] Can't Post

Warning: HTTP_REFERER can be forged. Do not use this as security.

If you have a URL, and you just want to extract the protocol and host, you can (and probably should) use the URI::URL module:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


use URI::URL;
my $url = URI::URL->new("http://www.pobox.com/~japhy/");
$host = $url->host;
if ($host eq "www.pobox.com") {
print "everything went as planned"
}
else {
print "something's messed up"
}
</pre><HR></BLOCKQUOTE>

You might want to do:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$front = $url->scheme . "://" . $url->host;
</pre><HR></BLOCKQUOTE>

to get the "http://www.pobox.com".

Or, you can roll your own solution:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


($front) = $ENV{HTTP_REFERER} =~ m{
( # save to $1 (and return)
[^:]* # 0 or more non-: characters
:// # '://'
[^/]* # 0 or more non-/ characters
)
}x;
</pre><HR></BLOCKQUOTE>

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives