473,372 Members | 1,326 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,372 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;
  7. sub print_login_form($);
  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");
  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;
  20. unless (param("filled")) {
  21.     print_login_form("");
  22.     exit;
  23. }
  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};
  31. # Successful Match
  32. print redirect(- location=>"upload.html");
  34. sub print_login_form($) {
  35.     print <<END1;
  36. <HTML>
  37. <HEAD>
  38. <TITLE> Media File Upload/Search System</TITLE>
  39. </HEAD>
  41. <body bgcolor="#003366">
  42. <form ACTION="cgi-bin/login.cgi" METHOD="POST">
  43. <img src="unilogo.gif" border=0> </img>
  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>
  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>
  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>
  59. </TABLE>
  61. <style>
  62. .box{
  63. margin-left: 165px;
  64. }
  65. </style>
  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>
  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>
  82. </TABLE>
  83. </FORM>
  84. </BODY>
  85. </HTML>
  86. END1
  87. }
  90. sub dieNice {
  91.     my ($msg) = @_;
  92.     print "<h2>Error</h2>\n";
  93.     print $msg;
  94.     exit;
  95. }
  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. }
  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. }
Mar 21 '07 #1
5 5712
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
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:

<input type="hidden" name="filled" value="1">

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
  3. use strict;
  4. use CGI qw(:standard);
  5. use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
  6. use DBI;
  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");
  13. my $ps = param('ps');
  14. my $user = param('username');
  15. my $pass = param('password');
  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;
  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};
  28.     print redirect(location=>"upload.html");
  29. }
  31. print <<END1;
  32. <HTML>
  33. <HEAD>
  34. <TITLE> Media File Upload/Search System</TITLE>
  35. </HEAD>
  37. <body bgcolor="#003366">
  38. <form NAME="login" ACTION="cgi-bin/login.cgi" METHOD="POST">
  39. <img src="unilogo.gif" border=0> </img>
  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>
  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>
  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>
  55. </TABLE>
  57. <style>
  58. .box{
  59. margin-left: 165px;
  60. }
  61. </style>
  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>
  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>
  79. </TABLE>
  80. </FORM>
  81. </BODY>
  82. </HTML>
  83. END1
  86. sub dienice {
  87.     my ($msg) = @_;
  88.     print "<h2>Error</h2>\n";
  89.     print $msg;
  90.     exit;
  91. }
  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. }
Mar 25 '07 #4
4,059 Expert 2GB
what does happen when you run the script?
Mar 26 '07 #5
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

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...
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...
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...
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...
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...
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...
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...
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...
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>
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.