#!/usr/local/bin/perl ########################################################################### # register: Register for Nile.com # Introducing cookies (this Perl script stores cookies) ########################################################################### use CGI "param"; @input = param(); # get all form field namess if ( ! @input ) { # if there are NO input fields $firstime = 'Y'; # it must be first time around } else { # otherwise $firstime = 'N'; # it's not the first time around $userid = param('userid'); # get form input for userid $pswd = param('pswd'); $pswd2 = param('pswd2'); $fname = param('fname'); $addr = param('addr'); &test_input(); # test the input fields } if ($firstime eq 'N' && $msg eq '') { # if not firstime and no errors ®ister_user(); # register a new user &set_cookie(); # set userid cookie } &print_form(); # re-print the form exit(0); ########################################################################### sub print_form { print "Content-type: text/html \n"; print "\n"; print ""; print < Nile.com - Registeration
Welcome to Nile.com
Customer Registration
Registration
Enter a User Id $u_error
Enter a Password $p_error
$p_error Retype
Enter Full Name $n_error
Enter Address $a_error

$msg
EOF } ########################################################################### sub test_input { if ($pswd ne $pswd2) { $msg = 'Password fields must be the same!'; $p_error = '***'; } if ($userid eq '') { $msg = 'Please enter required field(s) above!'; $u_error = '***'; } if ($pswd eq '' or $pswd2 eq '') { $msg = 'Please enter required field(s) above!'; $p_error = '***'; } if ($fname eq '') { $msg = 'Please enter required field(s) above!'; $n_error = '***'; } if ($addr eq '') { $msg = 'Please enter required field(s) above!'; $a_error = '***'; } } ############################################################################### sub register_user { open (CUSTOMER, "< ../../data/nile.cust.file") || print ("Cannot open file - $!"); while ($rec = ) { chomp($rec); ($DBuserid) = split(":", $rec); if ($userid eq $DBuserid) { $msg = 'User id already exists. Please choose another!'; $u_error = '***'; return; # get out from subroutine } } close (CUSTOMER); open (CUSTOMER, ">> ../../data/nile.cust.file") || print ("Cannot open file - $!"); $addr =~ s/\r\n/__/g; $rec = join(":",($userid,$pswd,$fname,$addr) ); print CUSTOMER "$rec\n"; close (CUSTOMER); $msg = "Registration Successful - Welcome to Nile.com"; $OK = "Y"; } ############################################################################## sub set_cookie { return if(! $OK); # if not OK, get out print "Set-Cookie: custid=$userid; path=/ \n"; # save cookie } ##############################################################################