473,434 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

Simple Login Form - Stuck and Frustrated

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
5 5719
KevinADC
4,059 Expert 2GB
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
1,089 Expert 1GB
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
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
4,059 Expert 2GB
what does happen when you run the script?
Mar 26 '07 #5
miller
1,089 Expert 1GB
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

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

Similar topics

3
by: janet | last post by:
hI ... I am newbie to ASP ... i am trying to write a code to accept login and passwor from a user and verify through a table created in MySQL. I am just trying to write a code on my own...
2
by: harris | last post by:
I am needing some help with a few asp pages and a login script. Let me start by describing my project. I am trying to connect to a database and verify a users login and password, then based on...
10
by: amit.purohit | last post by:
hi, I have a very strange problem on my login Page. the Page was working fine a few days back, but now does not generate post back events for controls. this login page uses form based...
4
by: ItNerd | last post by:
Someone PLEASE HELP ME!!!!! All I want to do is a simple postback and grab the value from a textbox on clicking a linkbutton like below, but the value is not writing to the screen. I am...
14
by: n8 | last post by:
Hi, Hi have to do the followign and have been racking my brain with various solutions that have had no so great results. I want to use the System.Net.WebClient to submit data to a form (log a...
4
by: Phillip Vong | last post by:
I'm a newbie using VS2005 to learn and test. I'm testing against the Northwind DB. I created a simple variable called "myVariable" and I assigned it the character "c". I want my SQL query to use...
6
by: cyndithomas via AccessMonster.com | last post by:
I new to Visual Basic and am struck on a issue. Have created a Login in Screen for Remote User to access and input data. Want the Remote to be able to login & access the Input Form, and Manager...
3
JamieHowarth0
by: JamieHowarth0 | last post by:
Hi folks, Got a bit of an interesting question. I'm in the process of learning ASP.NET using Microsoft's CTP of Visual Web Studio "Orcas". Part of my classic ASP website incorporates a login...
2
by: sukatoa | last post by:
I am a newbie on JavaScript.... I would like to know about what's wrong in my code below: <html> <head> <title>Text Example</title> </head> <body>
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.