13 Replies - 3842 Views - Last Post: 23 November 2007 - 12:55 PM

#1 rustix   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 10-November 07

Help needed with writing user input to a text file using Perl and CGI

Posted 21 November 2007 - 03:30 AM

Hi.. I'm creating a new user registration page (like the ones you find when you register on a forum) using Perl (that is with CGI). As I have to store the new user's information I decided to write the user's information to a text file stored on the server. The following is the coding i used to write the information to a text file. There are 5 input boxes the values of which i want it written to the text file:
  • Name
  • Username
  • Password
  • Confirm Password
  • Date of Birth


code used to save the information:
#!c:\Perl\bin\Perl.exe
$file = "registration.txt";
$formdata = $ENV{'QUERY_STRING'};
($name, $username) = split(/=/,$formdata);

print "Content-Type: text/html\n\n";
print "<html>";
print "<body>";
print "Hello $name";
print "</body></html>";

if (open (OUTFILE, "> $file"))
{
	print OUTFILE "name = $name\n";
}



However i only seem to be able to save the name of the user (basically only the first variable). How could I store the rest?

Are there any suggestions or improvements that could be recommended?

All help is greatly appreciated asap.
Thanks in advance....

Is This A Good Question/Topic? 0
  • +

Replies To: Help needed with writing user input to a text file using Perl and CGI

#2 fahlyn   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 03-November 07

Re: Help needed with writing user input to a text file using Perl and CGI

Posted 21 November 2007 - 07:19 AM

Personally, for writing a Perl CGI app, i highly reccoment CGI.pm.


so
 use CGI; 



then you can change your app to be like this

#!c:\Perl\bin\Perl.exe
use CGI;
$file = "registration.txt";

#DON"T NEED WITH CGI   $formdata = $ENV{'QUERY_STRING'};
#DON'T NEED WITH CGI   ($name, $username) = split(/=/,$formdata);

my $cgi = CGI->new;

print "Content-Type: text/html\n\n";
print "<html>";
print "<body>";
print "Hello " . $cgi->param('name');
print "</body></html>";

if (open (OUTFILE, "> $file"))
{
	print OUTFILE "name = " . $cgi->param('name') ."\n";
	print OUTFILE "name = " . $cgi->param('username') ."\n";
	print OUTFILE "name = " . $cgi->param('otherquerystringparam') ."\n";
}





another thing that I would highly recommend for a CGI application is using either HTML::Template or Template::Toolkit. Those are both really great tools that allow you to keep your code and presentation of data separate. I personally prefer Template::Toolkit, but for something as simple as you're doing here HTML::Template would be less work (less to learn).
Was This Post Helpful? 0
  • +
  • -

#3 girasquid   User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: Help needed with writing user input to a text file using Perl and CGI

Posted 21 November 2007 - 08:20 AM

Another way to do it would be to store the parameters passed to your script into a hash before using them, like so:
#!/usr/bin/perl -w
use strict;
use CGI qw(:cgi);

my $file = 'registration.txt';
my $q = new CGI;
my %A = $q->Vars;	# store arguments into %A
print $q->header();
print qq ~
<html>
<body>
Hello, $A{name}
</body>
</html>
~;
if(open(OUTFILE,">$file")) {
	print OUTFILE qq ~
name = $A{name}\n
	~;
}



I can second Fahlyn's recommendation of HTML::Template or some other templating module - I use HTML::Template a lot, and it's great for keeping HTML and code separated from one another.
Was This Post Helpful? 0
  • +
  • -

#4 rustix   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 10-November 07

Re: Help needed with writing user input to a text file using Perl and CGI

Posted 21 November 2007 - 10:14 AM

hey guys thanks.. but there are a few clarifications though.. what the "->" operator do and what does the "~" operator do?
Was This Post Helpful? 0
  • +
  • -

#5 rustix   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 10-November 07

Re: Help needed with writing user input to a text file using Perl and CGI

Posted 21 November 2007 - 10:42 AM

guys another problem... i can only store one record in the file.. when i click submit to save the registration info the file always overwritten... how could i stop this? i mean the file isn't suppose to overwrite cuz in that case i won't be able to store the info of all the users but i'll only be able to store the most recent new user... what could i do to overcome this prob?
Was This Post Helpful? 0
  • +
  • -

#6 girasquid   User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: Help needed with writing user input to a text file using Perl and CGI

Posted 21 November 2007 - 11:12 AM

If you want to append to the file, use this instead:
open(OUTFILE,">>$file")


The ~ operator is being used with qq to provide the same functionality as double quotes(variable interpolation), but allow me to write all of what I want to print at once on multiple lines, without multiple print statements. It's to cut down on extra typing.

The -> operator is for calling methods on the CGI object that was created and stored into either $q in my sample code, or $cgi in fahlyn's code.
Was This Post Helpful? 0
  • +
  • -

#7 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Help needed with writing user input to a text file using Perl and CGI

Posted 21 November 2007 - 09:59 PM

View Postgirasquid, on 21 Nov, 2007 - 11:12 AM, said:

The ~ operator is being used with qq to provide the same functionality as double quotes(variable interpolation), but allow me to write all of what I want to print at once on multiple lines, without multiple print statements. It's to cut down on extra typing.

The -> operator is for calling methods on the CGI object that was created and stored into either $q in my sample code, or $cgi in fahlyn's code.


the "~" character is not an operator, it is an arbitrary delimiter that lets the qq operator know where the string begins and ends. I recommend you use {} with qq and q (and other quote and quote-like operators):

print qq{this will be printed};

perl counts in/out paired brackets when used with quoting operators, but it does not with single character delimiters like "~".

You can also use double/single-quotes to print multiple lines. All qq does is add variable interpolation and allows you to use double-quotes without escaping them.

The -> is an operator, commonly called the arrow operator for obvious reasons. It is used for dereferencing references. In the case of:

$q->Vars

$q is an object, which is the same as a reference in perl. Vars is a method (a function) of the $q object. So the arrow operator must be used to tell perl to call the Var function of the $q object (a CGI object). It is also used to dereference other things:

$v = [qw(foo bar baz)];
print $v->[0];# prints "foo";

$v is a reference to an array. You (rustix) will learn about references as your perl education continues.

This post has been edited by KevinADC: 21 November 2007 - 10:06 PM

Was This Post Helpful? 0
  • +
  • -

#8 rustix   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 10-November 07

Re: Help needed with writing user input to a text file using Perl and CGI

Posted 22 November 2007 - 01:58 AM

thanks you guys! you've all been a great help!
Was This Post Helpful? 0
  • +
  • -

#9 rustix   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 10-November 07

Re: Help needed with writing user input to a text file using Perl and CGI

Posted 22 November 2007 - 06:39 AM

guys what could possibly be wrong with the following set of coding?

#!c:\Perl\bin\Perl.exe
use CGI;
$file = "registration.txt";
my $cgi = CGI->new;

open(INFILE , "registration.txt");

$mark = 1;
$valUser = $cgi->param('username');
$valPass = $cgi->param('password');

while($line = <INFILE>)
{
	($name, $username, $password) = split(/=/ , $line);
	if( $username eq $valUser && $password == $valPass)
	{
		$mark=0;
		$re=CGI->new();
		print $re->redirect("success.pl");
		last;
	}

}



basically i want it to check whether the username exists in the text file and if it does then it should check whether the associated password is also corrected typed in by the user, but i get an "Internal Server Error" message. Why is this?

I tried compiling it using ActivPerl but there weren't any errors during compilation.. What could i do to solve this prob?

P.S. - the format the data is written to the file is :
"name=username=password=confirmedPassword=dateOfBirth

please help dude... a few hours left to the deadline!

This post has been edited by rustix: 22 November 2007 - 06:41 AM

Was This Post Helpful? 0
  • +
  • -

#10 girasquid   User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: Help needed with writing user input to a text file using Perl and CGI

Posted 22 November 2007 - 07:45 AM

That's because you aren't printing out a header.

You need to add this:
print $cgi->header();


Before you print out anything else.

This is a common issue when you're writing CGI scripts, and one of those things you just have to keep track of.
Was This Post Helpful? 0
  • +
  • -

#11 rustix   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 10-November 07

Re: Help needed with writing user input to a text file using Perl and CGI

Posted 22 November 2007 - 10:44 AM

Nope that didn't work either... All i got was :

Quote

Content-Type: text/html; charset=ISO-8859-1


on the new page. i did how ever try printing an output at different points of the code and i got the output at all points except for the print placed inside the "if" function.

why is this?
Was This Post Helpful? 0
  • +
  • -

#12 girasquid   User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: Help needed with writing user input to a text file using Perl and CGI

Posted 22 November 2007 - 03:03 PM

That's probably because you're doing something wrong in terms of splitting up what you read out of the file for comparisons. Try adding an else statement to your conditional that prints something(like what it tried to compare, for example), and see if that's getting executed.
Was This Post Helpful? 0
  • +
  • -

#13 rustix   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 10-November 07

Re: Help needed with writing user input to a text file using Perl and CGI

Posted 23 November 2007 - 11:42 AM

well guys i submitted it in today.. thanks again for all your help! :)
Was This Post Helpful? 0
  • +
  • -

#14 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Help needed with writing user input to a text file using Perl and CGI

Posted 23 November 2007 - 12:55 PM

View Postgirasquid, on 22 Nov, 2007 - 07:45 AM, said:

That's because you aren't printing out a header.

You need to add this:
print $cgi->header();


Before you print out anything else.

This is a common issue when you're writing CGI scripts, and one of those things you just have to keep track of.



Look back at his code, it does print a header. A redirection header. If he prints a content header then the redirection header will not work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1