By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,974 Members | 1,913 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,974 IT Pros & Developers. It's quick & easy.

Simple Login Form - Stuck and Frustrated

P: 2
Hi, i just started learning perl which i would use for my uni. project unfortunately. Well, this is a simple user login page i tried to create but i cant get it to work. Can someone please check this out. Corrections would be highly appreciated. The code:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use CGI qw(:standard);
  3. use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
  4. use DBI;
  5. $CGI::POST_MAX = 1024 * 1000;
  6.  
  7. sub print_login_form($);
  8.  
  9. my $dbh = DBI->connect('DBI:mysql:users:localhost', 'root', '', {
  10.     RaiseError    => 1,
  11.     AutoCommit    => 1
  12. }) or &dieNice("Can't connect to database:$DBI::errstr");
  13.  
  14. my $username = param('username');
  15. my $password = param('password');
  16. my $data = $dbh->prepare("select * from users where username=?") or &dbdie;
  17. $data->execute($username) or &dbdie;
  18. my $uinfo = $data->fetchrow_hashref;
  19.  
  20. unless (param("filled")) {
  21.     print_login_form("");
  22.     exit;
  23. }
  24.  
  25. # Error Checking
  26. dieNice("Please enter your Username") if !defined $username;
  27. dieNice("Please enter your Password") if !defined $password;
  28. dieNice("The username $username does not exist.") if $username ne $uinfo->{username};
  29. dieNice("Incorrect password") if $password ne $uinfo->{password};
  30.  
  31. # Successful Match
  32. print redirect(- location=>"upload.html");
  33.  
  34. sub print_login_form($) {
  35.     print <<END1;
  36. <HTML>
  37. <HEAD>
  38. <TITLE> Media File Upload/Search System</TITLE>
  39. </HEAD>
  40.  
  41. <body bgcolor="#003366">
  42. <form ACTION="cgi-bin/login.cgi" METHOD="POST">
  43. <img src="unilogo.gif" border=0> </img>
  44.  
  45. <font color="yellow"><h1>Media File Upload/Search System</h1></font>
  46. <font color="white"><h3>Sign into File Upload/Search System</h3></font>
  47.  
  48. <TABLE>
  49. <TR>
  50. <TD ALIGN="right"><font color="white"><STRONG>Username:</STRONG><font></TD>
  51. <TD><INPUT TYPE="text" SIZE=30 NAME="username"></TD>
  52. </TR>
  53.  
  54. <TR>
  55. <TD ALIGN="right"><font color="white"><STRONG>Password:</STRONG><font></TD>
  56. <TD><INPUT TYPE="password" SIZE=30 NAME="password"></TD>
  57. </TR>
  58.  
  59. </TABLE>
  60.  
  61. <style>
  62. .box{
  63. margin-left: 165px;
  64. }
  65. </style>
  66.  
  67. <TABLE class=box>
  68. <TR>
  69. <TD>
  70. <a href=newuser.html><FONT color=c0c0c0 ><strong>New User</font></a>
  71. <input type="submit" name="submit" value="Login">
  72. </TD>
  73. </TR>
  74.  
  75. <TABLE>
  76. <TR>
  77. <TD ALIGN="left">
  78. <a href=deleteuser.html><FONT color=c0c0c0 ><strong>Don't like this service, Delete User</font></a>
  79. </TD>
  80. </TR>
  81.  
  82. </TABLE>
  83. </FORM>
  84. </BODY>
  85. </HTML>
  86. END1
  87. }
  88.  
  89.  
  90. sub dieNice {
  91.     my ($msg) = @_;
  92.     print "<h2>Error</h2>\n";
  93.     print $msg;
  94.     exit;
  95. }
  96.  
  97. sub dbdie {
  98.     my ($package, $filename, $line) = caller;
  99.     my $errmsg  = "Database error: $DBI::errstr<br> called from $package $filename line $line";
  100.     &dieNice($errmsg);
  101. }
  102.  
  103. sub encrypt {
  104.     my ($plain) = @_;
  105.     my @salt = ('a'..'z', 'A'..'Z', '0'..'9', '.', '/');
  106.     return crypt($plain, $salt[int(rand(@salt))] . $salt[int(rand(@salt))]);
  107. }
  108.  
Mar 21 '07 #1
Share this Question
Share on Google+
5 Replies


KevinADC
Expert 2.5K+
P: 4,059
syntax error here:

Expand|Select|Wrap|Line Numbers
  1. Rai seError =>1

can't have a space like that, should be:

Expand|Select|Wrap|Line Numbers
  1. RaiseError =>1 

might have othe problems too but that needs correcting
Mar 21 '07 #2

miller
Expert 100+
P: 1,089
Hi olaamussah,

I took a moment and reformatted your code along with putting a CODE tag around it. Your approach to coding is rather unsophisticated, but that's not an entirely bad thing. The main thing that you could use is an introduction to a few coding conventions, like for naming of variables and functions.

Nevertheless, one thing that I noticed is that your code will currently never get to the verification of the username and password. This is because you look for an input field named "filled" as your key that the form has been submitted. There is no such field in your form, so the primary page will constantly be redisplayed.

One way to fix this is to add that key to your form:

[HTML]
<input type="hidden" name="filled" value="1">
[/HTML]

Another way to fix this would be to check the request method, to see if it equals "POST".

Anyway, make this change and you should be on your way.

- Miller
Mar 21 '07 #3

P: 2
I've been trying to make this work but still no progress, i think is not getting the entered values to be used in the perl code. Please help if you know what the problem is. Thanks in advance


Expand|Select|Wrap|Line Numbers
  1. #!C:/usr/bin/perl
  2.  
  3. use strict;
  4. use CGI qw(:standard);
  5. use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
  6. use DBI;
  7.  
  8. my $dbh = DBI->connect('DBI:mysql:users:localhost', 'root', '', {
  9.     RaiseError => 1,
  10.     AutoCommit => 1
  11. }) or &dienice("Can't connect to database:$DBI::errstr");
  12.  
  13. my $ps = param('ps');
  14. my $user = param('username');
  15. my $pass = param('password');
  16.  
  17.  
  18. if ($ps eq "login") {
  19.     my $data = $dbh->prepare("SELECT * FROM users WHERE username=?") or &dbdie;
  20.     $data->execute($user) or &dbdie;
  21.     my $uinfo = $data->fetchrow_hashref;
  22.  
  23.     &dienice("Please enter your Username") if !defined $user;
  24.     &dienice("Please enter your Password") if !defined $pass;
  25.     &dienice("The username $user does not exist.") if $user ne $uinfo->{username};
  26.     &dienice("Incorrect password") if $pass ne $uinfo->{password};
  27.  
  28.     print redirect(location=>"upload.html");
  29. }
  30.  
  31. print <<END1;
  32. <HTML>
  33. <HEAD>
  34. <TITLE> Media File Upload/Search System</TITLE>
  35. </HEAD>
  36.  
  37. <body bgcolor="#003366">
  38. <form NAME="login" ACTION="cgi-bin/login.cgi" METHOD="POST">
  39. <img src="unilogo.gif" border=0> </img>
  40.  
  41. <font color="yellow"><h1>Media File Upload/Search System</h1></font>
  42. <font color="white"><h3>Sign into File Upload/Search System</h3></font>
  43.  
  44. <TABLE>
  45. <TR>
  46. <TD ALIGN="right"><font color="white"><STRONG>Username:</STRONG><font></TD>
  47. <TD><INPUT TYPE="text" SIZE=30 NAME="username"></TD>
  48. </TR>
  49.  
  50. <TR>
  51. <TD ALIGN="right"><font color="white"><STRONG>Password:</STRONG><font></TD>
  52. <TD><INPUT TYPE="password" SIZE=30 NAME="password"></TD>
  53. </TR>
  54.  
  55. </TABLE>
  56.  
  57. <style>
  58. .box{
  59. margin-left: 165px;
  60. }
  61. </style>
  62.  
  63. <TABLE class=box>
  64. <TR>
  65. <TD>
  66. <a href="cgi-bin/newuser.cgi"><FONT color=c0c0c0 ><strong>New User</font></a>
  67. <input type="hidden" name="ps" value="login">
  68. <input type="submit" name="login" value="Login">
  69. </TD>
  70. </TR>
  71.  
  72. <TABLE>
  73. <TR>
  74. <TD ALIGN="left">
  75. <a href="cgi-bin/deleteuser.cgi"><FONT color=c0c0c0 ><strong>Don't like this service, Delete User</font></a>
  76. </TD>
  77. </TR>
  78.  
  79. </TABLE>
  80. </FORM>
  81. </BODY>
  82. </HTML>
  83. END1
  84.  
  85.  
  86. sub dienice {
  87.     my ($msg) = @_;
  88.     print "<h2>Error</h2>\n";
  89.     print $msg;
  90.     exit;
  91. }
  92.  
  93. sub dbdie {
  94.     my ($package, $filename, $line) = caller;
  95.     my $errmsg = "Database error: $DBI::errstr<br> called from $package $filename line $line";
  96.     &dienice($errmsg);
  97. }
  98.  
Mar 25 '07 #4

KevinADC
Expert 2.5K+
P: 4,059
what does happen when you run the script?
Mar 26 '07 #5

miller
Expert 100+
P: 1,089
Hi olaamussah,

We have already been helping you with this problem in another thread. Please ask any related questions in the same thread so that we can be sure that you've heard and applied the advice that we've previously given. Otherwise, we will not be encouraged to help you any further.

Also, take the time to put a CODE tag around your code. This will help persist the formatting and therefore make it much easier to read.

I'm merging the threads now and will reformat your message. If I have time later, I'll take another look at your problem.

- Miller
Mar 26 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.