Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Apr 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Adding to a textfile

    Hi

    I'm new to PERL and CGI.

    I've been trying to create a program which takes the username and password output from a form and adds this to the records of a textfile, after checking that the same username isn't already included.

    However, although the username is added to the records the passord isn't, and I don't see why.

    below is the CGI file...

    #!/usr/bin/perl

    &Parse_Form;

    $urnm = $formdata{'urnm'};
    $pwrd = $formdata{'pwrd'};

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

    open (LOG, "<smcreg1304logfile.txt") || &ErrorMessage;
    while (<LOG>) {
    ($ssnm, $pwrd) = split /~/;

    $window = time - $oldtime;
    if ($ssnm eq $urnm) {
    print "This Username already exists: ", $ssnm, "<P>\n";
    exit;
    }
    }

    $ssnm = $urnm;
    $record = $ssnm."~".$pwrd;

    open (LOG, ">>smcreg1304logfile.txt") || &ErrorMessage;
    flock(LOG, 2);
    print LOG "$record\n";
    flock(LOG, 8);
    close (LOG);

    print "<center>\n";
    print "<P><B>Thank you for using Dating Made Simple</B><BR><BR><BR><BR>\n";

    sub ErrorMessage {
    print "Content-type: text/html\n\n";
    print "The server can't open the file. It either doesn't exist or the permissions are wrong. \n";
    exit;
    }

    sub Parse_Form {
    if ($ENV{'REQUEST_METHOD'} eq 'GET') {
    @pairs = split(/&/, $ENV{'QUERY_STRING'});
    } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
    read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    @pairs = split(/&/, $buffer);

    if ($ENV{'QUERY_STRING'}) {
    @getpairs =split(/&/, $ENV{'QUERY_STRING'});
    push(@pairs,@getpairs);
    }
    } else {
    print "Content-type: text/html\n\n";
    print "<P>Use Post or Get";
    }

    foreach $pair (@pairs) {
    ($key, $value) = split (/=/, $pair);
    $key =~ tr/+/ /;
    $key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

    $value =~s/<!--(.|\n)*-->//g;

    if ($formdata{$key}) {
    $formdata{$key} .= ", $value";
    } else {
    $formdata{$key} = $value;
    }
    }
    }
    1;

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,519
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    I didn't test any of it out, but this might be something to try:
    Keep your original in case I got it all messed up though.

    Instead of this:

    open (LOG, "<smcreg1304logfile.txt") || &ErrorMessage;
    while (<LOG>) {
    ($ssnm, $pwrd) = split /~/;
    $window = time - $oldtime;
    if ($ssnm eq $urnm) {
    print "This Username already exists: ", $ssnm, "<P>\n";
    exit;
    }
    }


    Try this:

    open (LOG, "<smcreg1304logfile.txt") || &ErrorMessage;
    @ODB=<LOG>;
    close (LOG);
    foreach $rec (@ODB) {
    ($ssnm, $pwrd) = split(/~/,$rec);
    $window = time - $oldtime;
    if ($ssnm eq $urnm) {
    print "This Username already exists: ", $ssnm, "<P>\n";
    exit;
    }
    }

  • #3
    Regular Coder
    Join Date
    Sep 2004
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts
    mlseim - what was wrong with the way it was before? thats how i'd do it...


    Tsost - couple of things. Firstly, are you getting the pswd data from the form correctly? if there is a spelling mistake in your form or anything like that you might be looking for the data with the wrong name. SImple way to check that is after:
    $urnm = $formdata{'urnm'};
    $pwrd = $formdata{'pwrd'};
    put this:
    print "Content-type: text/html\n\n";
    print $urnm."~".$pwrd;
    exit;

    if it prints both username and password you are getting both bits of data and you need to look else where. Its a good debugging technique to go through biut by bit printing stuff out like this if you arn't sure where the data is going. It can help you narrow down the problem area.

    Second, I know this is abit of a jump forward but its baring in mind as soon as you start programming. you need to check your inputed data for things that are going to screw up your program of pose a security risk. In this case if someone were to put a ~ char in there username or password (unlikly as it may be) then you will end up with 2 ~ in one line of your username file and this will confuse the hell out of your program later on. You could just simply check to see if theres any non word chars

    if ($usnm =~ /\W/)
    {
    print "You're an idiot, stop trying to hack my site"; #or something to that effect
    exit;
    }

    \W will match non word chars. word ones are A-Z 0-9 and underscore _ which should be all you need

    i don't need to go any further but if you want some more tips heres a couple.
    for your error subroutine print this:
    print "The server can't open the file. $! \n";
    $! should show you the last error that happened which if you are opening a file will tell you why it didn't happen.
    lastly, look into using the CGI module. very easy to use in its most basic form and has so many things you will find usful in the future if you carry on programming you'll wonder how you ever lived without it

    hth

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,519
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    andeyed ...

    I know it should work, but this is something I have never gotten an answer for ....

    I have done a couple of websites on different webhosts. I have used the very
    SAME Perl script on each. It works on one, but not the other. So, I
    discovered if I open the file, read into an array, close the file ... then use
    foreach $rec (@ODB) it works ...

    Both webhosts are using Perl 5+ ... I can't explain it.

    There are other instances where I have noticed differences also.

    Again, I get a script, it works on one webhost, but not the other.

    Sometimes it's a module thing, like: use CGI
    ... or I might have to use: use CGI ':standard';

    --- so, I'm just trying to help him out by trying some different things

  • #5
    Regular Coder
    Join Date
    Sep 2004
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thats fair enough, i've never had that problem before...not that i remember anyway.

    then i don't think i have ever used just use CGI; and not CGI ':standard';

  • #6
    Regular Coder
    Join Date
    Sep 2003
    Posts
    290
    Thanks
    0
    Thanked 0 Times in 0 Posts
    $urnm = $formdata{'urnm'};
    $pwrd = $formdata{'pwrd'};

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

    open (LOG, "<smcreg1304logfile.txt") || &ErrorMessage;
    while (<LOG>) {
    ($ssnm, $pwrd) = split /~/;

    $window = time - $oldtime;
    if ($ssnm eq $urnm) {
    print "This Username already exists: ", $ssnm, "<P>\n";
    exit;
    }
    }
    You are overwriting the value in this variable when you use it for two different things.

  • #7
    Regular Coder
    Join Date
    Sep 2003
    Posts
    290
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Also, just a few comments for good programming practices.

    * Always run under strict and warnings.
    * Always use the CGI module over custom parsing routines.
    * Always use the flock constants (e.g., LOCK_SH, LOCK_EX) instead of hard coded numbers.

    There is a free online book that may help in learning to program in Perl.
    http://learn.perl.org/library/beginning_perl/

  • #8
    New Coder
    Join Date
    Apr 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Guys

    Thanks for all your help


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •