Click to See Complete Forum and Search --> : IP sent by email


kareem
02-14-2003, 08:40 PM
Hi Folks,
I need to have a code embeded on my website that either submits the visitor's ip by email either auto when a visitor provides the URL or by clicking a button on the webpage ..
please hlep.
have a loot at my post here
Click here (http://forums.webdeveloper.com/showthread.php?s=&threadid=4230)
Thanks

Mr Cgi Guy
02-14-2003, 11:46 PM
This will do that for you. Basically copy the code save it to a file named something like ipsend.cgi
set the permissions to 755...
you may need to change the sendmail path to whatever it is on your server... hopefully you have sendmail...and also, put in your email address.

i have it printing the word 'send' so that you could run it in a browser and see that it worked.

you'd probably want to remove that and execute it with a ssi call. and then it would be a silent script so to speak. of course, if you have any sort of traffic coming to your site, you'll be bombarded with emailed ips.

#!/usr/bin/perl


print "Content-type: text/html\n\n";
$sendmail = '/usr/sbin/sendmail';
$adminemail = "you\@yourdomain.com";

open (MAIL, "|$sendmail -t");
print MAIL "To: $adminemail\n";
print MAIL "From: $adminemail\n";
print MAIL "Subject: Ip Address\n\n";
print MAIL "$ENV{REMOTE_ADDR}\n";
close (MAIL);

print "sent";

jeffmott
02-15-2003, 08:40 AM
You have some pointless quoting resulting in the need for backslashes, the pipe to sendmail isn't very portable if the server has something other than sendmail, and of course the usual bad programming practices.

#!/usr/bin/perl -wT

use strict;
use warnings FATAL => 'all';

my %config = (
    mailprog => '/usr/sbin/sendmail -t',
    mailto => ['you1@your.com', 'you2@your.com'],
    from => 'admin@your.com',
);

die 'REMOTE_ADDR has not been defined.' unless defined $ENV{REMOTE_ADDR};

for (@{$config{mailto}}) {
    open MAIL, "|$config{mailprog}" or die $!;
    print MAIL "To: $_\n",
               "From: $config{from}\n",
               "Subject: IP Address\n",
               "\n",
               $ENV{REMOTE_ADDR};
    close MAIL or warn $!;
}

print "Content-Type: text/html\n\n";
print 'Mail sent.';

As CGI Guy said, though, this will bring in a river of emails. A better choice would be to store the IP addresses in a database.