<?xml version="1.0" encoding="utf-8" ?><rss version="2.0" xml:base="https://www.webmaster-forums.net/crss/node/1012603" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title></title>
    <link>https://www.webmaster-forums.net/crss/node/1012603</link>
    <description></description>
    <language>en</language>
          <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/print-text-file#comment-1072967</link>
    <description> &lt;p&gt;Thanks again.&lt;/p&gt;
&lt;p&gt;Well im very stumped now. I tryed adding those to the script but dont know what im doing wrong because no matter what i get an internal server error. I&#039;m no perl whizz as you can see.&lt;/p&gt;
 </description>
     <pubDate>Wed, 18 Oct 2000 15:44:11 +0000</pubDate>
 <dc:creator>Technics</dc:creator>
 <guid isPermaLink="false">comment 1072967 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title>Apply Common Sense</title>
    <link>https://www.webmaster-forums.net/serverside-scripting/print-text-file#comment-1072958</link>
    <description> &lt;p&gt;Ok, so you&#039;re getting a server error.  When you used my code, did you:&lt;/p&gt;
&lt;p&gt;* make a variable called $IP_FILE that holds the path to the file you want to edit?&lt;br /&gt;
* call the function as addIP($addr), where $addr is a scalar holding an IP address?&lt;br /&gt;
* make sure the web user can write to the IP address file?&lt;br /&gt;
* check the error log to see WHY the program died?&lt;/p&gt;
&lt;p&gt;I suggest you include this line of code near the top of your program:&lt;/p&gt;
&lt;p&gt;&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;use CGI::Carp &amp;#039;fatalsToBrowser&amp;#039;;&lt;/code&gt;&lt;/div&gt;&#039;&lt;/p&gt;
&lt;p&gt;Assuming you HAVE the CGI::Carp module, this will send fatal errors to your browser, instead of just being less-than-helpful.&lt;/p&gt;
 </description>
     <pubDate>Wed, 18 Oct 2000 14:33:13 +0000</pubDate>
 <dc:creator>japhy</dc:creator>
 <guid isPermaLink="false">comment 1072958 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/print-text-file#comment-1072957</link>
    <description> &lt;p&gt;Thanks guys for your help but both codes both cause internal server errors &lt;img src=&quot;https://www.webmaster-forums.net/misc/smileys/sad.png&quot; title=&quot;Sad&quot; alt=&quot;Sad&quot; class=&quot;smiley-content&quot; /&gt;.&lt;/p&gt;
 </description>
     <pubDate>Wed, 18 Oct 2000 14:25:30 +0000</pubDate>
 <dc:creator>Technics</dc:creator>
 <guid isPermaLink="false">comment 1072957 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title>Ground Zero</title>
    <link>https://www.webmaster-forums.net/serverside-scripting/print-text-file#comment-1072956</link>
    <description> &lt;p&gt;I really think we need to start from scratch here.  WHAT does the file holding the IP addresses look like?  Is it just one IP address per line?  Or is there more data?&lt;/p&gt;
&lt;p&gt;Next, all you want to do is see whether or not the IP address is in the file, and if it is not, add it?&lt;/p&gt;
&lt;p&gt;If I have these basic ideas down correctly, then I suggest you try the following code.  I&#039;m going to present in stages.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Code 1.0&lt;/strong&gt;&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;sub addIP {&lt;br /&gt;&amp;nbsp; my $to_add = shift;&lt;br /&gt;&amp;nbsp; my $found = 0;&lt;br /&gt; &lt;br /&gt;&amp;nbsp; # look for it in the file&lt;br /&gt;&amp;nbsp; open FILE, $IP_FILE or die &amp;quot;can&amp;#039;t open $IP_FILE: $!&amp;quot;;&lt;br /&gt;&amp;nbsp; while (&amp;lt;FILE&amp;gt;) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; chomp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ($_ eq $to_add) { $found = 1; last; }&lt;br /&gt;&amp;nbsp; }&lt;br /&gt;&amp;nbsp; close FILE;&lt;br /&gt; &lt;br /&gt;&amp;nbsp; # add it to the file if it wasn&amp;#039;t there&lt;br /&gt;&amp;nbsp; if (not $found) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; open FILE, &amp;quot;&amp;gt;&amp;gt; $IP_FILE&amp;quot; or die &amp;quot;can&amp;#039;t append to $IP_FILE: $!&amp;quot;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print FILE &amp;quot;$to_add\n&amp;quot;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; close FILE;&lt;br /&gt;&amp;nbsp; }&lt;br /&gt;}&lt;/code&gt;&lt;/div&gt;&#039;&lt;/p&gt;
&lt;p&gt;This code saves us the trouble of reading through the entire file if the IP address is already in there, due to me calling last() if the IP address is found.  This code can be made more efficient, though, by opening the file for reading AND writing:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Code 2.0&lt;/strong&gt;&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;sub addIP {&lt;br /&gt;&amp;nbsp; my $to_add = shift;&lt;br /&gt;&amp;nbsp; my $found = 0;&lt;br /&gt; &lt;br /&gt;&amp;nbsp; # look for it in the file&lt;br /&gt;&amp;nbsp; open FILE, &amp;quot;+&amp;lt; $IP_FILE&amp;quot; or die &amp;quot;can&amp;#039;t open $IP_FILE for read/write: $!&amp;quot;;&lt;br /&gt;&amp;nbsp; seek FILE, 0, 0;&amp;nbsp; # ensure we&amp;#039;re at the beginning&lt;br /&gt;&amp;nbsp; while (&amp;lt;FILE&amp;gt;) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; chomp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ($_ eq $to_add) { $found = 1; last; }&lt;br /&gt;&amp;nbsp; }&lt;br /&gt; &lt;br /&gt;&amp;nbsp; # add it if not found&lt;br /&gt;&amp;nbsp; if (not $found) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print FILE &amp;quot;$to_add\n&amp;quot;;&lt;br /&gt;&amp;nbsp; }&lt;br /&gt;&amp;nbsp; close FILE;&lt;br /&gt;}&lt;/code&gt;&lt;/div&gt;&#039;&lt;/p&gt;
&lt;p&gt;This is better, since we only open the file once, and it still works as before.  I&#039;m going to streamline things a bit, to make it look a bit cleaner:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Code 2.1&lt;/strong&gt;&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;sub addIP {&lt;br /&gt;&amp;nbsp; my $to_add = shift;&lt;br /&gt;&amp;nbsp; my $found = 0;&lt;br /&gt; &lt;br /&gt;&amp;nbsp; # look for it in the file&lt;br /&gt;&amp;nbsp; open FILE, &amp;quot;+&amp;lt; $IP_FILE&amp;quot; or die &amp;quot;can&amp;#039;t open $IP_FILE for read/write: $!&amp;quot;;&lt;br /&gt;&amp;nbsp; seek FILE, 0, 0;&amp;nbsp; # ensure we&amp;#039;re at the beginning&lt;br /&gt;&amp;nbsp; while (&amp;lt;FILE&amp;gt;) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; chomp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; last if $found = ($_ eq $to_add);&amp;nbsp; # XXX&lt;br /&gt;&amp;nbsp; }&lt;br /&gt; &lt;br /&gt;&amp;nbsp; # add it if not found&lt;br /&gt;&amp;nbsp; print FILE &amp;quot;$to_add\n&amp;quot; if not $found;&lt;br /&gt; &lt;br /&gt;&amp;nbsp; close FILE;&lt;br /&gt;}&lt;/code&gt;&lt;/div&gt;&#039;&lt;/p&gt;
&lt;p&gt;The line I marked with XXX is pretty nifty -- if $_ and $to_add are the same, then $_ eq $to_add is true, and returns 1.  Otherwise, it&#039;s false, and returns &#039;&#039;.  In turn, $found gets set to the value returned by that test, so if the test returns 1, $found is set to 1, and we can exit the block.&lt;/p&gt;
&lt;p&gt;There&#039;s another thing I&#039;d suggest adding, which is file locking.  This is so that if two people run your program at once, you needn&#039;t worry about your file getting corrupted.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Code 3.0&lt;/strong&gt;&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;use Fcntl &amp;#039;LOCK_EX&amp;#039;;&amp;nbsp; # import the LOCK_EX constant&lt;br /&gt; &lt;br /&gt;sub addIP {&lt;br /&gt;&amp;nbsp; my $to_add = shift;&lt;br /&gt;&amp;nbsp; my $found = 0;&lt;br /&gt; &lt;br /&gt;&amp;nbsp; # look for it in the file&lt;br /&gt;&amp;nbsp; open FILE, &amp;quot;+&amp;lt; $IP_FILE&amp;quot; or die &amp;quot;can&amp;#039;t open $IP_FILE for read/write: $!&amp;quot;;&lt;br /&gt;&amp;nbsp; flock FILE, LOCK_EX;&amp;nbsp; # lock this file&lt;br /&gt;&amp;nbsp; seek FILE, 0, 0;&amp;nbsp; # ensure we&amp;#039;re at the beginning&lt;br /&gt;&amp;nbsp; while (&amp;lt;FILE&amp;gt;) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; chomp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; last if $found = ($_ eq $to_add);&lt;br /&gt;&amp;nbsp; }&lt;br /&gt; &lt;br /&gt;&amp;nbsp; # add it if not found&lt;br /&gt;&amp;nbsp; print FILE &amp;quot;$to_add\n&amp;quot; if not $found;&lt;br /&gt; &lt;br /&gt;&amp;nbsp; close FILE;&lt;br /&gt;&amp;nbsp; # file gets unlocked automatically&lt;br /&gt;}&lt;/code&gt;&lt;/div&gt;&#039;&lt;/p&gt;
&lt;p&gt;And last, but not least, let&#039;s localize the filehandle, so we can get rid of the $found variable too, as well as have Perl close the filehandle for us:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Code 3.1&lt;/strong&gt;&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;use Fcntl &amp;#039;LOCK_EX&amp;#039;;&amp;nbsp; # import the LOCK_EX constant&lt;br /&gt; &lt;br /&gt;sub addIP {&lt;br /&gt;&amp;nbsp; my $to_add = shift;&lt;br /&gt;&amp;nbsp; local *FILE;&amp;nbsp; # localize the filehandle&lt;br /&gt; &lt;br /&gt;&amp;nbsp; # look for it in the file&lt;br /&gt;&amp;nbsp; open FILE, &amp;quot;+&amp;lt; $IP_FILE&amp;quot; or die &amp;quot;can&amp;#039;t open $IP_FILE for read/write: $!&amp;quot;;&lt;br /&gt;&amp;nbsp; flock FILE, LOCK_EX;&amp;nbsp; # lock this file&lt;br /&gt;&amp;nbsp; seek FILE, 0, 0;&amp;nbsp; # ensure we&amp;#039;re at the beginning&lt;br /&gt;&amp;nbsp; while (&amp;lt;FILE&amp;gt;) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; chomp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return if $_ eq $to_add;&amp;nbsp; # return if found&lt;br /&gt;&amp;nbsp; }&lt;br /&gt; &lt;br /&gt;&amp;nbsp; # if we got here, it couldn&amp;#039;t have been found&lt;br /&gt;&amp;nbsp; print FILE &amp;quot;$to_add\n&amp;quot;;&lt;br /&gt;&amp;nbsp; # file gets unlocked automatically&lt;br /&gt;}&lt;/code&gt;&lt;/div&gt;&#039;&lt;/p&gt;
&lt;p&gt;Ok, that&#039;s all for now.&lt;/p&gt;
 </description>
     <pubDate>Wed, 18 Oct 2000 14:20:35 +0000</pubDate>
 <dc:creator>japhy</dc:creator>
 <guid isPermaLink="false">comment 1072956 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/print-text-file#comment-1072945</link>
    <description> &lt;p&gt;Ok, scratch that. It MIGHT work, i just noticed you didn&#039;t do a file to string right away, but it&#039;s best to be safe.&lt;/p&gt;
 </description>
     <pubDate>Wed, 18 Oct 2000 06:24:56 +0000</pubDate>
 <dc:creator>Orpheus</dc:creator>
 <guid isPermaLink="false">comment 1072945 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/print-text-file#comment-1072944</link>
    <description> &lt;p&gt;nope, that wont work&lt;/p&gt;
&lt;p&gt;it will only do the first line of the file&lt;/p&gt;
&lt;p&gt;try this&lt;/p&gt;
&lt;p&gt;&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;sub removeaddr {&lt;br /&gt;local($/);&lt;br /&gt;open (FILE, &amp;quot;$banfile&amp;quot;);&lt;br /&gt;$ThePage = join(&amp;#039;|&amp;#039;, &amp;lt;FILE&amp;gt;;&lt;br /&gt;close (FILE);&lt;br /&gt;&lt;br /&gt;$ThePage =~ /(.*)$input{&amp;#039;ipnumber&amp;#039;}|(.*)/;&lt;br /&gt;$ThePage = $1 . $2;&lt;br /&gt;$ThePage =~ s/\|/\n/g;&lt;br /&gt;&lt;br /&gt;open(FILE, &amp;quot;&amp;gt;$banfile&amp;quot;);&lt;br /&gt;print FILE $ThePage;&lt;br /&gt;close(FILE);&lt;br /&gt;}&lt;br /&gt;} &lt;/code&gt;&lt;/div&gt;&#039;&lt;/p&gt;
 </description>
     <pubDate>Wed, 18 Oct 2000 06:22:29 +0000</pubDate>
 <dc:creator>Orpheus</dc:creator>
 <guid isPermaLink="false">comment 1072944 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/print-text-file#comment-1072941</link>
    <description> &lt;p&gt;I don&#039;t understand?&lt;/p&gt;
&lt;p&gt;your saying that this won&#039;t work?&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;open (FILE, &amp;quot;$banfile&amp;quot;);&lt;br /&gt;$ThePage = join(&amp;#039;|&amp;#039;, &amp;lt;FILE&amp;gt;;&lt;br /&gt;close (FILE);&lt;br /&gt;&lt;br /&gt;$ThePage =~ s/\|/\n/g;&lt;br /&gt;print $ThePage;&lt;/code&gt;&lt;/div&gt;&#039;&lt;/p&gt;
&lt;p&gt;Try it...  I&#039;ve used it before and it&#039;s worked fine.&lt;/p&gt;
&lt;p&gt;what does &lt;strong&gt;local();&lt;/strong&gt; do?&lt;br /&gt;
I&#039;ve seen &lt;strong&gt;$_&lt;/strong&gt; but not &lt;strong&gt;$\&lt;/strong&gt; ... do you mean &lt;strong&gt;local($_);&lt;/strong&gt;?&lt;/p&gt;
 </description>
     <pubDate>Wed, 18 Oct 2000 03:39:09 +0000</pubDate>
 <dc:creator>Mark Hensler</dc:creator>
 <guid isPermaLink="false">comment 1072941 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/print-text-file#comment-1072936</link>
    <description> &lt;p&gt;You can&#039;t push the entire contents of a file into a file into a string without saying local($\); first. &lt;/p&gt;
&lt;p&gt;Just thought i&#039;d point that out. &lt;img src=&quot;https://www.webmaster-forums.net/misc/smileys/smile.png&quot; title=&quot;Smiling&quot; alt=&quot;Smiling&quot; class=&quot;smiley-content&quot; /&gt;&lt;/p&gt;
 </description>
     <pubDate>Wed, 18 Oct 2000 00:48:30 +0000</pubDate>
 <dc:creator>Orpheus</dc:creator>
 <guid isPermaLink="false">comment 1072936 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/print-text-file#comment-1072926</link>
    <description> &lt;p&gt;You want to make a script to remove IPs from the list?&lt;/p&gt;
&lt;p&gt;I&#039;ll use the same variable for the IP and let you make a new subroutin to put it in...&lt;br /&gt;
&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;sub removeaddr {&lt;br /&gt;&lt;br /&gt;open (FILE, &amp;quot;$banfile&amp;quot;);&lt;br /&gt;$ThePage = join(&amp;#039;|&amp;#039;, &amp;lt;FILE&amp;gt;;&lt;br /&gt;close (FILE);&lt;br /&gt;&lt;br /&gt;$ThePage =~ /(.*)$input{&amp;#039;ipnumber&amp;#039;}|(.*)/;&lt;br /&gt;$ThePage = $1 . $2;&lt;br /&gt;$ThePage =~ s/\|/\n/g;&lt;br /&gt;&lt;br /&gt;open(FILE, &amp;quot;&amp;gt;$banfile&amp;quot;);&lt;br /&gt;print FILE $ThePage;&lt;br /&gt;close(FILE);&lt;br /&gt;} #END unless&lt;br /&gt;&lt;br /&gt;} #END sub&lt;/code&gt;&lt;/div&gt;&#039;&lt;/p&gt;
&lt;p&gt;Logic:&lt;br /&gt;
1) replace &quot;|&quot; with newline characters&lt;br /&gt;
2) match the pattern [wild card] IP [wild card]&lt;br /&gt;
3) make $ThePage the first wild card and the second (thus, removing the IP)&lt;br /&gt;
4) put $ThePage back into the file&lt;/p&gt;
 </description>
     <pubDate>Tue, 17 Oct 2000 22:22:33 +0000</pubDate>
 <dc:creator>Mark Hensler</dc:creator>
 <guid isPermaLink="false">comment 1072926 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/print-text-file#comment-1072899</link>
    <description> &lt;p&gt;thanks it works. &lt;/p&gt;
&lt;p&gt;Whats the command to edit a text file from a cgi script?. To also remove the banned ip from the list. Instead of logging into ftp and removing it.&lt;/p&gt;
&lt;p&gt;Thank you for your help.&lt;/p&gt;
 </description>
     <pubDate>Tue, 17 Oct 2000 14:13:51 +0000</pubDate>
 <dc:creator>Technics</dc:creator>
 <guid isPermaLink="false">comment 1072899 at https://www.webmaster-forums.net</guid>
  </item>
  </channel>
</rss>
