检查字母数字字符并从HTML表单获取输入 [英] Checking for alphanumeric characters and getting input from HTML form

查看:171
本文介绍了检查字母数字字符并从HTML表单获取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Perl中编程相当新,而且我有几个编译问题,我似乎无法解决。我的程序从这个HTML表单获取输入。



问题:我的表单应该使用post还是get方法?

 < FORM action =./ cgi-bin / Perl.plmethod =GET> 
< br>
全名:< br>< input type =textname =full_namemaxlength =20>< br>
使用者名称:< br>< input type =textname =user_namemaxlength =8>< br>
密码:< br>< input type =passwordname =passwordmaxlength =15>< br>
确认密码:< br>< input type =passwordname =new_passwordmaxlength =15>< br>

我打开一个CSV文件,将user_name的值写入一个数组,然后执行一些检查用户的输入。



问题1:我需要检查full_name,user_name,password和new_password是否都是字母数字或空格,但我一直收到如下的多个错误:

 在Perl.pl中使用未初始化的值$ full_name字符串eq第33行

我不认为我已经正确使用CGI从表单中获取这些值。我也相信我没有正确地检查字母数字字符。如何解决此问题?



问题2:如果用户的密码不匹配,我需要将用户重定向到特定的网页,如果用户名已被占用。我使用了元重定向,但没有成功。



这是我的代码:

 <$ c如何显示正确的错误页面? $ c>#!/ usr / bin / perl 
使用CGI qw(:standard);
使用strict;
使用警告;

printContent-type:text / html\\\
\\\
;

#opening Members.csv阅读
my $ file ='/home/2014/amosqu/public_html/cgi-bin/Members.csv';
open(my $ csv,'&',$ file)||死无法打开你的文件;

#从HTML表单获取这些值
my $ full_name = param('full_name');
my $ user_name = param('user_name');
my $ password = param('password');
my $ new_password = param('new_password');

我的@users =();

#分割每一行csv文件
foreach(< $ csv>){
chomp;
my @fields = split(/ \,/);
push @users,$ fields [1]; #输入阵列中的所有用户名
}

close $ csv;

#opening Members.csv用于追加
open(my $ fh,'>>',$ file)||死无法打开你的文件;

#问题1
#如果(($ full_name&& $ user_name&&& $ password&& $ new_password)eq / \A [[:alnum:]] + \z /){
#如果密码不匹配,则重定向到错误页面
if($ password ne $ new_password){
print qq(< html> \\\
);
print qq(< head> \\\
);
print qq(< title>密码不匹配。< / title> \\\
);
print qq {< meta http-equiv =refreshcontent =5; URL =http://www.cs.mcgill.ca/~amosqu/registration.html> \\\
} ;
print qq(< / head> \\\
);
print qq(< body> \\\
);
print qq(< b>< center>密码不符合< / b>< / center> \\\
\\\
);
打印qq(< / body> \\\
);
打印qq(< / html> ; \\\
);
}
#如果匹配,检查用户名是否在Members.csv中
else {
if(grep(/ $ user_name / (< html> \\\
);
print qq(< head> \\\
);
print qq(< title>对不起< / title> \\\
);
print qq {< meta http-equiv =refreshcontent =5; URL =http://www.cs.mcgill.ca /~amosqu/registration.html\">\\\
};
print qq(< / head> \\\
);
print qq(< body> \\\
);
print qq(< b>< center>用户名已被占用。< / b>< / center> \\\
\\\
);
print qq(< / body> \\\
);
print qq(< / html> \\\
);
}
#如果它不在Members.csv中,则将值附加到文件
else {
print $ fh$ full_name,$ user_name,$ password \\\
;
}
}
}

close $ fh;


解决方案

这应该会让你走。你的代码有很多问题不会阻止它的运行,但是目前的观点并不是使用 CGI ,所以我会和你一起滚动。除非您有充分理由使用,否则使用 GET >
$ b


  • 问题是这里

     

    ($ full_name&& $ user_name&& $ password&& $ new_password)eq / \A [[:alnum:]] + \ z /){

    您正在使用布尔值&& 结合三个变量的真值的操作,并检查它是否等于匹配 $ _ 对这个正则表达式。



    您必须单独检查每个变量,并使用绑定操作符 =〜 对照正则表达式来测试它们。使用POSIX字符类也是不好的形式。我建议你使用 grep ,就像这样

      my $ mismatch = grep / [^ A-Z0-9] / i,$ full_name,$ user_name,$ password,$ new_password; 

    现在, $ mismatch 如果任何变量包含非字母数字字符,则为true 。 (严格来说,它被设置为具有非字母数字字符的变量的数字,如果它们中的任何一个都不为零,则为零(假)。)



    然后你可以说:

    $ p $ if(not $ mismatch){...}


  • 看起来您只需要一个 else 页面。



I'm fairly new to programming in Perl and I have a couple of compilation issues I can't seem to resolve. My program gets input from this HTML form.

Question: Should my form use the post or get method?

<FORM action="./cgi-bin/Perl.pl" method="GET">
     <br>
     Full name: <br><input type="text" name="full_name" maxlength="20"><br>
     Username: <br><input type="text" name="user_name" maxlength="8"><br>
     Password: <br><input type="password" name="password" maxlength="15"><br>
     Confirm password: <br><input type="password" name="new_password" maxlength="15"><br>

I open a CSV file, write the value of user_name into an array and do a number of checks on the user's input.

Problem #1: I need to check that full_name, user_name, password, and new_password are all alphanumeric or a space but I keep getting multiple errors that look like:

Use of uninitialized value $full_name in string eq at Perl.pl line 33

I don't think I've used CGI correctly to get these values from the form. I also believe I'm not correctly checking for alphanumeric characters. How can I resolve this?

Problem #2: I need to redirect the user to a specific webpage if their passwords don't match and if the username is already taken. I used a meta redirect but it's not doing it successfully. How can I display a proper error page?

This is my code:

#!/usr/bin/perl 
use CGI qw(:standard);
use strict;
use warnings;

print "Content-type: text/html\n\n";

#opening Members.csv for reading
my $file = '/home/2014/amosqu/public_html/cgi-bin/Members.csv';
open(my $csv, '<', $file)  || die "Could not open your file";

#getting these from HTML form
my $full_name = param('full_name');
my $user_name= param('user_name');
my $password = param('password');
my $new_password = param('new_password');

my @users = ();

#splitting each line of csv file
foreach (<$csv>) {
     chomp;
     my @fields = split (/\,/);
     push @users, $fields[1]; #put all usernames inside of array
}

close $csv;

#opening Members.csv for appending
open(my $fh, '>>', $file) || die "Could not open your file";

#SOURCE OF PROBLEM 1
#checking that all values are alphanumeric
if(($full_name && $user_name && $password && $new_password) eq /\A[[:alnum:]]+\z/) {
      #if passwords don't match, redirect to error page
      if($password ne $new_password){
         print qq(<html>\n);
         print qq(<head>\n);
         print qq(<title> Passwords don't match. </title> \n);
         print qq{<meta http-equiv="refresh"content="5;URL="http://www.cs.mcgill.ca/~amosqu/registration.html">\n};
         print qq(</head>\n);
         print qq(<body>\n);
         print qq(<b><center> Passwords don't match </b></center>\n\n);
         print qq(</body>\n);
         print qq(</html>\n);
     }
      #if they do match, check that user name isn't in Members.csv
      else { 
          if(grep (/$user_name/, @users)) {
             print qq(<html>\n);
             print qq(<head>\n);
             print qq(<title> Sorry username already taken. </title>\n);
             print qq{<meta http-equiv="refresh"content="5;URL="http://www.cs.mcgill.ca/~amosqu/registration.html">\n};
             print qq(</head>\n);
             print qq(<body>\n);
             print qq(<b><center> Username already taken. </b></center>\n\n);
             print qq(</body>\n);
             print qq(</html>\n);
        }
        #if it isn't already in Members.csv append values to the file
        else { 
             print $fh "$full_name, $user_name, $password \n";
       }
    }
}

close $fh;

解决方案

This should get you going. There is a number of issues with your code that don't stop it from working, but current wisdom is not to use CGI at all so I will roll with you.

  • Use GET unless you have a good reason to use POST

  • The problem is here

    if(($full_name && $user_name && $password && $new_password) eq /\A[[:alnum:]]+\z/) {
    

    You are using a Boolean && operation that combines the truth of the three variables, and checking whether that, as a string, is equal to the result of matching the contents of $_ against that regular expression.

    You must check each of the variables individually, and use the binding operator =~ to test them against a regex. It is also bad form to use the POSIX character classes. I suggest you use grep, like this

    my $mismatch = grep /[^A-Z0-9]/i, $full_name, $user_name, $password, $new_password;
    

    Now, $mismatch is true if any of the variables contain a non-alphanumeric character. (Strictly, it is set to the number of variables that have a a non-alphanumeric character, which is zero (false) if none of them do.)

    Then you can say

    if (not $mismatch) { ... }
    

  • It looks like you just need an else that builds a separate page.

这篇关于检查字母数字字符并从HTML表单获取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆