Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IP Logging from an Input Form

Status
Not open for further replies.

Ectoman

Technical User
Nov 30, 2001
8
US
I am haveing trouble figuring out how to get an IP address to be recorded, from a form. I want it to output to a log file. I've already gotten the script written and working, I just need help to figure that out.

I know of the "$REMOTE_ADDR" SSI command, but do not know how I would impliment that into the CGI.
 
#!/usr/local/bin/perl
use CGI qw/:new/;
my $cgi = new CGI;
print $cgi->header, $cgi->start_html,
&quot;<p>$ENV{'HTTP_REMOTE_ADDR'}</p>&quot;,
$cgi->end_html;



' hope this helps. If you are new to Tek-Tips, please use descriptive titles, check the FAQs,
and beware the evil typo.
 
Thanks goBoating.

Daniel, sorry. I did not see the Perl forum.
 
Me again. I am running into problems figuring out how to make this script work. I am a total newbie, it is a wonder I've made it this far with my script. Here is just part of my large script:

Code:
use CGI qw/:new/;
my $cgi = new CGI;
print $cgi->header, $cgi->start_html,
      &quot;<p>$ENV{'HTTP_REMOTE_ADDR'}</p>&quot;,
      $cgi->end_html;


open (IP,&quot;>>$ipname&quot;);
print IP <<EndOfHTML;
$FORM{'title'} $FORM{'url'}
EndOfHTML
close(IP);

I want to allow the whole script to use the IP number, to check it with a list of bad IPs, and to log it. The log part of the script is shown above. How does the IP retrieval part of it fit with the log part?

Thanks,
AJ Quick
 
Just add the IP variable to the log file, for example:
open (IP,&quot;>>$ipname&quot;);
print IP <<EndOfHTML;
$FORM{'title'} $FORM{'url'} $ENV{'REMOTE_ADDR'}
EndOfHTML
close(IP);
To check for a bad IP, you can have the bad IP's in a array and use a foreach loop, like this:
my @badips = (&quot;127.0.0.1&quot;, &quot;221.231.45.23&quot;);
foreach my $badip (@badips)
{
if ($ENV{'REMOTE_ADDR'} eq $badip)
{
print (&quot;This is a bad IP&quot;);
}
}

//Daniel
 
I have included that &quot;$ENV{'REMOTE_ADDR'}&quot; in the log part of the script and tested it. It doesn't work. Where should the part goBoating posted be in the whole mix? Should it be $FORM{'REMOTE_ADDR'}? Instead? What exactly is the part goBoating posted susposed to do?

I understood the Bad IP array you posted, thanks for doing that.


AJ Quick
 
Do you have access to the IP addresses? To test that you could just try this script:
#!/usr/bin/perl
print (&quot;Content-type: text/plain\n\n&quot;);
print (&quot;Your IP is: $ENV{'REMOTE_ADDR'}&quot;);
If that script outputs an IP, I don't see what the problem might be.

//Daniel
 
It works! Thanks so very much guys!! I will always come here for my help. (in perl forum next time).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top