#!/usr/local/bin/perl ########################################################################### # register: Register for Nile.com # Introducing cookies (this Perl script stores cookies) ########################################################################### use CGI "param"; use DBI; @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'); $lname = param('lname'); $fname = param('fname'); $addr = param('addr'); $email = param('email'); &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  (SQL)
Customer Registration
Registration
Enter a User Id $u_error
Enter a Password $p_error
$p_error
Retype
Enter Last Name $n_error
$n_error
    First
Enter Address $a_error
Enter E-mail $e_error

$msg
EOF } ########################################################################### sub test_input { if ($pswd ne $pswd2) { $msg = 'Password fields must 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 ($lname eq '' or $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 { $dbh = DBI->connect("DBI:mysql:nile","nile","nile"); if (!defined($dbh)) { $msg = "Cannot open database connection - $DBI::errstr"; return; } $sql = "select cust_id, password from customer "; $sql .= " where cust_id = \'$userid\' "; $cursor = $dbh->prepare($sql); $rc = $cursor->execute(); ($DBcust_id, $DBpassword) = $cursor->fetchrow_array(); if ($userid eq $DBcust_id) { $msg = 'User id already exists. Please choose another!'; $u_error = '***'; return; # get out from subroutine } $sql = "insert into customer "; $sql .= "values (\'$userid\', \'$pswd\' , "; $sql .= " \'$lname\', \'$fname\', \'$addr\', \'$email\' )"; $rc = $dbh->do($sql); $dbh->disconnect(); $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 } ##############################################################################