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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting IP address info from a POST ?? 1

Status
Not open for further replies.

octar

Technical User
Oct 21, 2002
28
AU
Hi,

I have the following file that I'm trying to help a friend out with... basically it's a message board where people put posts in, lately some post are coming in that are not welcome.

What I want to do is find out how I can get the IP address of each person that puts a post up?? and incorporate this into the script? is this possible? All I want to do is log the message info and the IP address in a seperate file for reference. I have included ###### HERE where I think the info need to be inserted, as I'm very new to perl I just need to know the syntax.

thanks



#!/usr/bin/perl

use CGI ':cgi-lib';
$in = new CGI;
$q = $in->Vars;
$action = $q->{'action'};

$file = "../$error = "";


######################################################
$header = qq {
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Woodies Wonderland</title>
<link rel="StyleSheet" type="text/css" href="../mystyle.css">
</head>
<body>
<!-- Start of Body Table -->
<table width="740" height="100%" border="0" align="center" cellpadding="1" cellspacing="2">
<tr>
<td align="center"valign="top">
<!-- Title Table -->
<table width="100%" height="66" border="0" align="center" cellpadding="1" cellspacing="2">
<tr>
<td height="66" align="center" class="pa_fg"><img src="../jpegs/Dev/title4.jpg" width="740" height="70" border="0" alt=""></td>
</tr>
</table>
<!-- ENd Title Table -->
<HR>
<table width="100%" border="0" height="20" align="center" cellpadding="1" cellspacing="0" summary="">
<tr>
<td class=""><A href = "../index.html" class="ilink">Home</A></td>
<td>&nbsp&nbsp</td>
<td class=""><A href = "draw.cgi" class="ilink">Draw</A></td>
<td>&nbsp&nbsp</td>
<td class=""><A href = "standing.cgi" class="ilink">Standing</A></td>
<td>&nbsp&nbsp</td>
<td class=""><A href = "../stats.html" class="ilink">Player Stats</A></td>
<td>&nbsp&nbsp</td>
<td class=""><A href = "../woodlotto.html" class="ilink">Wood Lotto</A></td>
<td>&nbsp&nbsp</td>
<td class=""><A href = "sledge.cgi"class="ilink">Sledge Ledger</A></td>
<td>&nbsp&nbsp</td>
<td class=""><A href = "travel.cgi" class="ilink">Travellers Tales</A></td>
<td>&nbsp&nbsp</td>
<td class=""><A href = "../gallery.html" class="ilink">Gallery</A></td>
</tr>
</table><HR>};
######################################################
$footer = qq {
<TABLE width = "100%" >
<TR>
<TD><A href = "../index.html">Home</A></TD>
<TD><A href = "draw.cgi">Draw</A></TD>
<TD><A href = "standing.cgi">Standings</A></TD>
<TD><A href = "../stats.html">Player Stats</A></TD>
<TD><A href = "../woodlotto.html">Woodlotto</A></TD>
<TD><A href = "sledge.cgi">Sledge Ledger</A></TD>
<TD><A href = "travel.cgi">Travellers Tales</A></TD>
<TD><A href = "gallery.cgi">Gallery</A></TD>
</TR>
</TABLE>
<HR>

</TD></TR>

</TABLE>

</BODY>};
######################################################
$form = qq{
<FORM METHOD = POST ACTION = "sledge.cgi">
};

$form3 = qq{
<FONT color = 0000aa size = 5>Post a Message!</FONT>
<TABLE width = 100% cellpadding = 0 cellspacing = 0 border = 0>

<TR>
<TD>Title:</TD>
<TD><INPUT TYPE = textbox NAME = "title" VALUE = "$q->{'title'}" MAXLENGTH = 50 SIZE = 50></TD>
</TR>

<TR>
<TD>Author:</TD>
<TD><INPUT TYPE = textbox NAME = "author" VALUE = "$q->{'author'}" MAXLENGTH = 50 SIZE = 50></TD>
</TR>

<TR>
<TD valign = top>Story:</TD>
<TD><TEXTAREA NAME = "body" rows = 6 cols = 60>$q->{'body'}</TEXTAREA></TD>
</TR>

</TABLE>
<input type=submit value="Post Message"> <input type=reset>
<input type=hidden name="action" value="update">
</FORM> <HR>
The last 30 messages displayed. Most recent messages are found at the top.<HR>\n
};


$form4 = qq{
<FONT color = 0000aa size = 5>Post a Message!</FONT>
<TABLE width = 550 cellpadding = 0 cellspacing = 0 border = 0>

<TR>
<TD>Title:</TD>
<TD><INPUT TYPE = textbox NAME = "title" VALUE = "" MAXLENGTH = 50 SIZE = 50></TD>
</TR>

<TR>
<TD>Author:</TD>
<TD><INPUT TYPE = textbox NAME = "author" VALUE = "" MAXLENGTH = 50 SIZE = 50></TD>
</TR>

<TR>
<TD valign = top>Story:</TD>
<TD><TEXTAREA NAME = "body" rows = 6 cols = 60></TEXTAREA></TD>
</TR>

</TABLE>
<input type=submit value="Post Message"> <input type=reset>
<input type=hidden name="action" value="update">
</FORM> <hr>
<p class="slstory">The last 30 messages displayed. Most recent messages are found at the top.</p><HR>\n

<hr>
};

######################################################

if ($action eq "update"){

if ( !$q->{'body'}) { $error = "Please add a message!";}
elsif (!$q->{'author'}) { $error = "Please enter the name of the author!";}
elsif (!$q->{'title'}) { $error = "There was no title for the message!";}
else
{
$old = $file;
$new = "$file.tmp";
$bak = "$file.bak";

open(OLD, "< $old") or die "can't open $old: $!";
open(NEW, "> $new") or die "can't open $new: $!";

$body = $q->{'body'};
$body =~ s/\n/<BR>/g;
$body =~ s/\%0D\%0A/<BR>/g;
print NEW qq{<TR><TD CLASS="sltitle">$q->{'title'}</TD><TD CLASS="slauthor">by $q->{'author'}, </TD></TR><TR><TD CLASS="slstory"colspan = 2>$body</TD></TR><TR><TD colspan = 2><HR></TD></TR><@>\r};

######## HERE $info = "../######## HERE
######## HERE
######## HERE

while (<OLD>) {(print NEW $_) or die "can't write to $new: $!";}

close(OLD) or die "can't close $old: $!";
close(NEW) or die "can't close $new: $!";

rename($old, $bak) or die "can't rename $old to $bak: $!";
rename($new, $old) or die "can't rename $new to $old: $!";
}
}

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

print $header;

print $form;

if ($error)
{print $form3; print "<FONT color = red size = 5>$error</FONT><HR>\n";}
else
{print $form4;}

open (DATA, "$file") or print "\n<B>can't open $file: $!</B>\n";

$i = 0;

print "<TABLE width = 740 border = 0>\n";

while (<DATA>) {
@bits = split (/<@>/);
for($j=0;$j<$#bits;$j++){
$lines[$i] = $bits[$j];
$i++;
}
}

for $i (0..29) {
print $lines[$i];
}
print "</TABLE>\n";

close (DATA);

print $footer;
 
Hi octar,

$ENV{'REMOTE_ADDR'} will capture the IP address information you want ...

you could embed the following code in your script:

open (FILE,">>$info"); # append log file
print FILE $ENV{'REMOTE_ADDR'},"\n";
close (FILE);

hope this helps ...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top