Gossamer Forum
Home : General : Perl Programming :

What country is the visitor from?

Quote Reply
What country is the visitor from?
Is it possible to find out where a visitor comes from and display an appropriate message for him/her, i.e.

if the vistor is from Australia/NZ
display appropriate message
if the visitor is from UK/Europe
display appropriate message
if the visitor is from USA/Canada
display appropriate message

Does anyone know how I could do this or could you direct me to a pre-built script somewhere?

Thanks.
Quote Reply
Re: What country is the visitor from? In reply to
Not really possible to do automatically. Well... you could take their host and do something based on the country code of the host (eg. blah.au, blah.co.uk, blah.co.jp, blah.ca). This will work with only some cases since lots of isp's just use .com regardless. Other than that, you can just prompt the user and save this info in a cookie for later visits (like most sites do). You could also use a hybrid of both those methods. If they're on a .com, then prompt them, otherwise show the page according to their country code...

Adrian
Quote Reply
Re: What country is the visitor from? In reply to
The old GT weather greeting script has been in the resources section for ages:

http://www.gossamer-threads.com/...r_Scripts/index.html

- Mark

Astro-Boy!!
http://www.zip.com.au/~astroboy/
Quote Reply
Re: What country is the visitor from? In reply to
Thanks for pointing me in the right direction Astroboy and thanks for your comments Adrian Smile

I've just tried running the script but I get these errors:

"use" may clash with future reserved word at homepage.pl line 12.
syntax error in file homepage.pl at line 12, next 2 tokens "use strict"
"my" may clash with future reserved word at homepage.pl line 16.
syntax error in file homepage.pl at line 16, next 2 tokens "my ("
"my" may clash with future reserved word at homepage.pl line 17.
syntax error in file homepage.pl at line 27, next 2 tokens "$country and"
Execution of homepage.pl aborted due to compilation errors.
"my" may clash with future reserved word at homepage.pl line 16.
syntax error in file homepage.pl at line 16, next 2 tokens "my ("
"my" may clash with future reserved word at homepage.pl line 17.
syntax error in file homepage.pl at line 27, next 2 tokens "$country and"
Execution of homepage.pl aborted due to compilation errors.


I'm running it through my hypermart account and the only thing I changed was the address to perl.

Do you know how I would go about fixing these errors?


Quote Reply
Re: What country is the visitor from? In reply to
What version of Perl are you running?

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: What country is the visitor from? In reply to
In the hypermart faq it says:

Where is Perl located on your server?
Perl 5.003 is located at /usr/local/bin/perl. For backwards compatibility we have perl 4 located at /usr/bin/perl. Make sure you use the right one for your script.

This is what I have:

#/usr/local/bin/perl -w

Quote Reply
Re: What country is the visitor from? In reply to
It's working now. I don't know what was going on before Blush

Quote Reply
Re: What country is the visitor from? In reply to
One thing though that isn't working is that it isn't going into this section:

if (defined $country and ($country =~ /Canada/i)) {
print qq|It's good to see someone else from Canada!|;
}
elsif (defined $country) {
print qq|What's it like in $country? Let me <a href="/feedback.htm">know</a>!\n"|;
}


You can see it running here: http://server5.hypermart.net/philipclark/homepage.pl

Code:

Code:
#!/usr/local/bin/perl -w

# Prints the Gossamer Threads Home Page greeting.
# Author: Alex Krohn (alex@gossamer-threads.com)

# NOTE: You need the list of domains to country names. I got mine from Analog and
# you can find it here:
#
# http://www.gossamer-threads.com/scripts/source/domains.tab
#

use strict;

print "Content-type: text/html\n\n";

my ($time, $greeting) = &get_date;
my $country = &get_country;

print qq~
<P><font face="arial,verdana,helvetica,tahoma" size=3><b>$greeting, and thanks for visiting
Gossamer Threads' website. Your interest in our site is appreciated.</b>

<P>Its $time in beautiful Vancouver, British Columbia, Canada where
Gossamer Threads is located. Country - $country
~;

if (defined $country and ($country =~ /Canada/i)) {
print qq|It's good to see someone else from Canada!|;
}
elsif (defined $country) {
print qq|What's it like in $country? Let me <a href="/feedback.htm">know</a>!\n"|;
}

exit;

sub get_country {
# --------------------------------------------------------------
my ($match, $code, $name);

if (exists $ENV{'REMOTE_HOST'} and ($ENV{'REMOTE_HOST'} =~ /\.(..)$/)) {
$match = $1;

open (DOMAINS, "domains.tab") or (print "SYSTEM ERR: CANT OPEN FILE" and exit);
while (<DOMAINS>) {
chomp;
($code, $name) = split (/:/);
($code eq $match) and close DOMAINS and return $name;
}
close DOMAINS;
}
return undef;
}

sub get_date {
# --------------------------------------------------------------
my @days = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
my @months = ('January','February','March','April','May','June','July',
'August','September','October','November','December');

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time - 10800);
($hour < 10) and ($hour = "0$hour");
($min < 10) and ($min = "0$min");
($sec < 10) and ($sec = "0$sec");

my $date = "$days[$wday] $months[$mon] $mday";
my ($greeting, $noon);

if ($hour < 12) { $greeting = "Good Morning"; }
elsif ($hour >=12 && $hour < 17) { $greeting = "Good Afternoon"; }
elsif ($hour >=17 && $hour < 24) { $greeting = "Good Evening"; }
else { $greeting = "Hello"; }

($hour < 12) and ($noon = "am");
($hour > 12) and ($hour = $hour - 12) and ($noon = "pm");
($hour == 00) and ($hour = 12);
($hour == 12) and ($noon = "pm");

return "$hour\:$min $noon on $days[$wday], $months[$mon] $mday", $greeting;
}
Quote Reply
Re: What country is the visitor from? In reply to
It sounds to me like you are actually using the Perl 4 - might want to check it again.

[Edit]
The line is missing a ! - it should be #!/usr/local/bin/perl not #/usr/local/bin/perl
[/Edit]

I have to wonder why they are still offering Perl 4 at /usr/bin/perl though - Perl 4 scripts will work just fine under Perl 5.

Oh well.

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: What country is the visitor from? In reply to
Yes I was using Perl 4 and not Perl 5. Blush

Quote Reply
Re: What country is the visitor from? In reply to
Did you grab the domains.tab file? That script seems to need it.

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: What country is the visitor from? In reply to
Yes I did: http://server5.hypermart.net/...ipclark/homepage.jpg

Quote Reply
Re: What country is the visitor from? In reply to
I don't think it contains anything for .com, .net, .org, etc. - only the country-specific TLD's.

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: What country is the visitor from? In reply to
Sorry Jason I don't get what you mean. Can you explain yourself further please?

--
edit
--

Shouldn't it display one of these two messages anyway?

It's good to see someone else from Canada!
What's it like in $country? Let me <a href="/feedback.htm">know</a>!

if (defined $country and ($country =~ /Canada/i)) {
print qq|It's good to see someone else from Canada!|;
}
elsif (defined $country) {
print qq|What's it like in $country? Let me <a href="/feedback.htm">know</a>!\n"|;
}
Quote Reply
Re: What country is the visitor from? In reply to
In your script, where you have:

Code:
Country - $country
Add this:

Code:
Host - $ENV{REMOTE_HOST}, address - $ENV{REMOTE_ADDR}
The script only works if you have a resolvable host, if the web server has hostname lookups enabled, and your resolvable hostname contains a two-character country code.

It's entirely possible that hypermart does not have this enabled - change your script to what I suggested and try it out - if you don't get $ENV{REMOTE_HOST} then unfortunately you might not be able to use the script.

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: What country is the visitor from? In reply to
In Reply To:
Shouldn't it display one of these two messages anyway?
No - it only displays a message if the country is canada or if the country is some other country. If the script didn't find a country, it won't show anything.

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: What country is the visitor from? In reply to
My host(paying) has a resolvable server so I'll host it there.

Thanks for all your help Jason. It was really appreciated Smile.

Quote Reply
Re: What country is the visitor from? In reply to
It doesn't matter so much if the server is resolvable, but whether or not the server resolves clients.

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: What country is the visitor from? In reply to
ok - thanks for clearing that up Smile