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

Read user name and password from text file.

Status
Not open for further replies.

vcherubini

Programmer
May 29, 2000
527
US
Hello. I wrote the following script in order to allow people to enter a forum, like this one, with a user name and password.<br><br><FONT FACE=monospace><br><br>#!usr/bin/perl -w<br><br># location of the txt file <br>$datadir = &quot;e:\\virtualhosts\\epicsoftware.com\\ collect the data from the user using Post<br><br>read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});<br>@pairs = split(/&/, $buffer);<br>foreach $pair (@pairs) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;($name, $value) = split(/=/, $pair);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$value =~ tr/+/ /;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$FORM{$name} = $value;<br>}<br><br>if ($FORM{'action'} eq &quot;checkpassword&quot;) { &checkpassword; }<br>else { &default; }<br><br>sub default {<br>print &quot;Content-type: text/html\n\n&quot;;<br>print &quot;&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Enter your user name and Password&lt;/title&gt;\n&lt;/head&gt;\n&quot;;<br>print &quot;&lt;body&gt;\n&quot;;<br>print &quot;&lt;form method=\&quot;post\&quot;&gt;\n&quot;;<br>print &quot;&lt;input type=\&quot;hidden\&quot; value=\&quot;checkpassword\&quot; name=\&quot;action\&quot;&gt;\n&quot;;<br>print &quot;&lt;font face=\&quot;verdana\&quot; size=\&quot;-2\&quot; color=\&quot;black\&quot;&gt;&lt;b&gt;username:&lt;/b&gt;&lt;/font&gt;\n&quot;;<br>print &quot;&lt;br&gt;&lt;input type=\&quot;text\&quot; name=\&quot;user\&quot; style=\&quot;background:00648E; color:white; height:20; font-size:10px;\&quot;&gt;&lt;br&gt;\n&quot;;<br>print &quot;&lt;font face=verdana size=-2 color=black&gt;&lt;b&gt;password:&lt;/b&gt;&lt;/font&gt;\n&quot;;<br>print &quot;&lt;br&gt;&lt;input type=\&quot;password\&quot; name=\&quot;password\&quot; style=\&quot;background:00648E; color:white; height:20; font-size:10px;\&quot;&gt;&lt;br&gt;\n&quot;;<br>print &quot;&lt;input type=\&quot;submit\&quot; value=\&quot;Enter\&quot; style=\&quot;background:black; color:white; height:20; font-size:10px;\&quot;&gt;\n&quot;;<br>print &quot;&lt;input type=\&quot;Reset\&quot; value=\&quot;Clear\&quot; style=\&quot;background:black; color:white; height:20; font-size:10px;\&quot;&gt;\n&quot;;<br>print &quot;&lt;/form&gt;\n&quot;;<br>print &quot;&lt;/body&gt;\n&lt;/html&gt;\n&quot;;<br>}<br>sub checkpassword {<br><br>open(FILE, &quot;$datadir/data.txt&quot;) ¦¦ die(&quot;File does not reside on server: $!&quot;);<br><br>while (&lt;FILE&gt;) {<br><br>@data = split(/\n/);<br>foreach $entry (@data) {<br><br>($user, $password) = split(/,/, $entry);<br><br>if ($name eq &quot;$user&quot;) {<br><br>$userverified = &quot;1&quot;;<br>}<br> if ($password eq &quot;$password&quot;) {<br> <br> $passwordverified = &quot;1&quot;;<br> <br> <br> }<br> }<br> }<br> close(FILE);<br> <br> if ($userverified && $passwordverified) {<br> <br>&accessgranted;<br><br>} elsif ($userverified) {<br><br>&badpassword;<br><br>} else {<br><br>&baduserpass;<br><br>}<br>}<br><br>sub accessgranted {<br><br>print &quot;&lt;html&gt;&lt;head&gt;&lt;title&gt;access granted&lt;/title&gt;&lt;/head&gt;\n&quot;;<br>print &quot;&lt;body&gt;\n&quot;;<br>print &quot;&lt;h1&gt;access granted&lt;/h1&gt;\n&quot;;<br>print &quot;&lt;/body&gt;\n&quot;;<br>print &quot;&lt;/html&gt;\n&quot;;<br><br>}<br><br>sub badpassword {<br><br>print &quot;&lt;html&gt;&lt;head&gt;&lt;title&gt;bad password&lt;/title&gt;&lt;/head&gt;\n&quot;;<br>print &quot;&lt;body&gt;\n&quot;;<br>print &quot;&lt;h1&gt;bad password&lt;/h1&gt;\n&quot;;<br>print &quot;&lt;/body&gt;\n&quot;;<br>print &quot;&lt;/html&gt;\n&quot;;<br><br>}<br><br>sub baduserpass {<br><br>print &quot;&lt;html&gt;&lt;head&gt;&lt;title&gt;bad username and password&lt;/title&gt;&lt;/head&gt;\n&quot;;<br>print &quot;&lt;body&gt;\n&quot;;<br>print &quot;&lt;h1&gt;bad username and password&lt;/h1&gt;\n&quot;;<br>print &quot;&lt;/body&gt;\n&quot;;<br>print &quot;&lt;/html&gt;\n&quot;;<br><br>}<br><br><br></font><br><br>When I upload and run the script, I get the following error:<br><br><FONT FACE=monospace><br><br>CGI Error<br>The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are:<br><br><br>Name &quot;main::pass&quot; used only once: possible typo at e:\virtualhosts\epicsoftware.com\ line 45.<br>Name &quot;main::user&quot; used only once: possible typo at e:\virtualhosts\epicsoftware.com\ line 47.<br>Use of uninitialized value at e:\virtualhosts\epicsoftware.com\ line 9.<br>Use of uninitialized value at e:\virtualhosts\epicsoftware.com\ line 47,&nbsp;&nbsp;chunk 1.<br>Use of uninitialized value at e:\virtualhosts\epicsoftware.com\ line 51,&nbsp;&nbsp;chunk 1.<br>Use of uninitialized value at e:\virtualhosts\epicsoftware.com\ line 51,&nbsp;&nbsp;chunk 1.<br>Use of uninitialized value at e:\virtualhosts\epicsoftware.com\ line 47,&nbsp;&nbsp;chunk 2.<br>Use of uninitialized value at e:\virtualhosts\epicsoftware.com\ have never gotten an error like this one before. Any help on decoding the error and my code will be greatly appreciated.<br><br>Thanks.<br><br>Vic
 
It appears that the errors you are getting are the result of some sloppiness in the code and the use of the '-w' switch in the first line of the program.&nbsp;&nbsp;In oversimplified terms, the '-w' tells Perl to critique the code.&nbsp;&nbsp;Some of what it looks at includes variables which are used only once.&nbsp;&nbsp;If a variable is used only once, then it may not be needed (probably is not needed).<br><br>You can take one of two approaches.<br>Best, look at the complaints and fix them.<br>Not as good, remove the '-w' from the first line of the program.<br><br>Suggestion:&nbsp;&nbsp;keep the -w and fix the sloppiness in the code.<br><br>'hope this helps. <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
goBoating:<br><br>Thanks for that. Yeah I whipped the script up really quick this morning. <br><br>Thanks for the help though. <br><br>-vic
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top