Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use CGI qw(:standard);
- use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
- use DBI;
- use strict;
- my $cgi = new CGI;
- my $fname;
- my $lname;
- my $email;
- my $userid;
- my $register;
- my @error;
- $register = $cgi->param('login');
- if ($register) {
- if ($cgi->param('fname') && $cgi->param('lname') && $cgi->param('email')) {
- #Copy user input into variables in the program
- $fname = $cgi->param('fname');
- $lname = $cgi->param('lname');
- $email = $cgi->param('email');
- # Validate user input
- my $ok_data;
- $ok_data = &validate_data;
- #If data is good, generate a userid
- if ($ok_data == 1) {
- $userid = &create_id;
- #connect to the database;
- my $dbh = DBI->connect("DBI:mysql:host=db-mysql;database=int322_101b06;port=zenit.senecac.on.ca:20684", "", "") or die("Can't connect to database.");
- #Get the table "users"
- #Select all data and try to match lname||userid. If lname||userid does not
- # exist, then lname||userid can be stored.
- my $sth = $dbh->prepare("SELECT * FROM users");
- $sth->execute;
- my $username_taken = 0;
- my @result;
- while (($username_taken == 0) && (@result = $sth->fetchrow_array())) {
- if (($lname == $result[2]) && ($userid == $result[0])) {
- $username_taken = 1;
- }
- }
- $dbh->disconnect();
- #If lname||userid already, exists, ask user for a new set of info
- if ($username_taken == 1) {
- push(@error, "Username already exists. Please register a different name.");
- &show_form(\@error);
- }
- else { #if lname and userid combo is unique, add to the database
- # Display user info back to the user
- &add_to_database;
- &repeat_info;
- }
- } #if ok_data != 1
- else {
- #Then @error would be filled. Pass this on to the form.
- &show_form(\@error);
- }
- }
- else {
- &show_form;
- }
- }
- sub add_to_database {
- my $dbh = DBI->connect("DBI:mysql:host=db-mysql;database=int322_101b06;port=zenit.senecac.on.ca:20684", "", "") or die("Can't connect to database.");
- my $sth = $dbh->prepare("INSERT INTO users VALUES(?,?,?,?)");
- $sth->execute($userid, $fname, $lname, $email);
- $sth->finish;
- $dbh->disconnect();
- my $userfile = $lname . $userid . '.' . 'acct';
- open FILE, ">accounts/$userfile";
- my $string = $userid . ',' . $fname . ',' . $lname . ',' . $email;
- print FILE $string;
- close(FILE);
- }
- sub repeat_info {
- print $cgi->header(),
- $cgi->start_html('Your account information'),
- $cgi->h3('Account information'),
- $cgi->p('You need your last name and userid to sign in next time'),
- $cgi->p('Userid: ', $userid);
- print $cgi->table({-border=>'0', -cellspacing=>'3', -cellpadding=>'3'},
- Tr(
- [
- td(['First Name: ', $fname]),
- td(['Last Name: ', $lname]),
- td(['Email: ', $email])
- ]
- )
- );
- print $cgi->a({-href=>"start.cgi"}, "Go here to start");
- print $cgi->end_html;
- }
- sub validate_data {
- #Validate first name
- if ($fname =~ /^[A-Za-z\ ]+$/) {
- push(@error, "ok-fname");
- }
- else {
- push(@error, "First name can only have letters<br/>");
- }
- #Validate last name
- if ($lname =~ /^[A-Za-z\ ]+$/) {
- push(@error, "ok-lname");
- }
- else {
- push(@error, "Last name can only have letters<br/>");
- }
- #Validate email
- if ($email =~ /^[A-Za-z:\/.~\?=\+&@]+$/){
- push(@error, "ok-email");
- }
- else {
- push(@error, "Email field has invalid characters<br/>");
- }
- #Count the number of errors in the register page
- my $i; #counter for the elements in the @error array
- my $numErrors = 3;
- for ($i = 0; $i < 3; $i++) {
- if ($error[$i] =~ /^ok/) {
- delete $error[$i];
- $numErrors -= 1;
- }
- }
- #If there are errors, display form with user input and msg where errors are
- if ($numErrors > 0) {
- return 0; #Return false, data is not good
- }
- else {
- return 1; #Return true, data is good
- }
- }
- sub show_form {
- print $cgi->header(),
- $cgi->start_html("Register - Soccer Pool"),
- $cgi->h3('Register to join the soccer pool');
- if (@error) {
- print join("", @error);
- }
- print $cgi->startform();
- print $cgi->table({-border=>'0', -cellspacing=>'3'},
- Tr(
- [
- td(['First Name', $cgi->textfield(-name=>'fname', -maxlength=>'25', -value=>"$fname")]),
- td(['Last Name', $cgi->textfield(-name=>'lname', -maxlength=>'25', -value=>"$lname")]),
- td(['Email', $cgi->textfield(-name=>'email', -maxlength=>'60', -value=>"$email")])
- ]
- )
- );
- print $cgi->submit(-value=>'Submit'), $cgi->reset();
- print $cgi->endform;
- print $cgi->end_html;
- }
- sub create_id {
- my $number = int(rand(9999999999));
- while (length($number) < 10) {
- $number = '0' . $number;
- }
- return $number;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement