Welcome to Dream.In.Code
Become an Expert!

Join 150,222 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,192 people online right now. Registration is fast and FREE... Join Now!




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

2 Pages V  1 2 >  
Reply to this topicStart new topic

Help needed with writing user input to a text file using Perl and CGI, Help needed asap! Deadline on 23/11

rustix
21 Nov, 2007 - 02:30 AM
Post #1

New D.I.C Head
*

Joined: 10 Nov, 2007
Posts: 13


My Contributions
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:
  1. Name
  2. Username
  3. Password
  4. Confirm Password
  5. Date of Birth


code used to save the information:
CODE

#!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....
User is offlineProfile CardPM
+Quote Post

fahlyn
RE: Help Needed With Writing User Input To A Text File Using Perl And CGI
21 Nov, 2007 - 06:19 AM
Post #2

New D.I.C Head
*

Joined: 3 Nov, 2007
Posts: 43


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


so
CODE
use CGI;



then you can change your app to be like this

CODE

#!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).


User is offlineProfile CardPM
+Quote Post

girasquid
RE: Help Needed With Writing User Input To A Text File Using Perl And CGI
21 Nov, 2007 - 07:20 AM
Post #3

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,295



Thanked: 18 times
Dream Kudos: 725
My Contributions
Another way to do it would be to store the parameters passed to your script into a hash before using them, like so:
CODE

#!/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.
User is offlineProfile CardPM
+Quote Post

rustix
RE: Help Needed With Writing User Input To A Text File Using Perl And CGI
21 Nov, 2007 - 09:14 AM
Post #4

New D.I.C Head
*

Joined: 10 Nov, 2007
Posts: 13


My Contributions
hey guys thanks.. but there are a few clarifications though.. what the "->" operator do and what does the "~" operator do?
User is offlineProfile CardPM
+Quote Post

rustix
RE: Help Needed With Writing User Input To A Text File Using Perl And CGI
21 Nov, 2007 - 09:42 AM
Post #5

New D.I.C Head
*

Joined: 10 Nov, 2007
Posts: 13


My Contributions
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?
User is offlineProfile CardPM
+Quote Post

girasquid
RE: Help Needed With Writing User Input To A Text File Using Perl And CGI
21 Nov, 2007 - 10:12 AM
Post #6

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,295



Thanked: 18 times
Dream Kudos: 725
My Contributions
If you want to append to the file, use this instead:
CODE

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.
User is offlineProfile CardPM
+Quote Post

KevinADC
RE: Help Needed With Writing User Input To A Text File Using Perl And CGI
21 Nov, 2007 - 08:59 PM
Post #7

D.I.C Head
Group Icon

Joined: 23 Jan, 2007
Posts: 238



Thanked: 6 times
Dream Kudos: 50
My Contributions
QUOTE(girasquid @ 21 Nov, 2007 - 11:12 AM) *

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 Nov, 2007 - 09:06 PM
User is offlineProfile CardPM
+Quote Post

rustix
RE: Help Needed With Writing User Input To A Text File Using Perl And CGI
22 Nov, 2007 - 12:58 AM
Post #8

New D.I.C Head
*

Joined: 10 Nov, 2007
Posts: 13


My Contributions
thanks you guys! you've all been a great help!
User is offlineProfile CardPM
+Quote Post

rustix
RE: Help Needed With Writing User Input To A Text File Using Perl And CGI
22 Nov, 2007 - 05:39 AM
Post #9

New D.I.C Head
*

Joined: 10 Nov, 2007
Posts: 13


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

CODE

#!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 Nov, 2007 - 05:41 AM
User is offlineProfile CardPM
+Quote Post

girasquid
RE: Help Needed With Writing User Input To A Text File Using Perl And CGI
22 Nov, 2007 - 06:45 AM
Post #10

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,295



Thanked: 18 times
Dream Kudos: 725
My Contributions
That's because you aren't printing out a header.

You need to add this:
CODE

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.
User is offlineProfile CardPM
+Quote Post

rustix
RE: Help Needed With Writing User Input To A Text File Using Perl And CGI
22 Nov, 2007 - 09:44 AM
Post #11

New D.I.C Head
*

Joined: 10 Nov, 2007
Posts: 13


My Contributions
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?
User is offlineProfile CardPM
+Quote Post

girasquid
RE: Help Needed With Writing User Input To A Text File Using Perl And CGI
22 Nov, 2007 - 02:03 PM
Post #12

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,295



Thanked: 18 times
Dream Kudos: 725
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 05:45AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month