使用 php 验证用户名、密码和电子邮件
我正在使用 php 验证用户名、通行证和电子邮件。我需要确保我做对了,这样就没有人可以绕过登录页面。
这是值:
$email=$_POST['email'];
$username=$_POST['uname'];
$passwd=$_POST['pass'];
$passwd2=$_POST['passcopy'];
到目前为止,我有电子邮件验证:
if(!(preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $_POST['email'])))
die(msg(0,"You haven't provided a valid email"));
密码等于:
if ($passwd != $passwd2) {
die(msg(0,"Passwords are not equal"));
}
密码长度:
if ((strlen($passwd) < 8) || (strlen($passwd) > 16)) {
die(msg(0,"Your password must be between 8 and 16 characters. Please type in a longer password"));
}
我知道我需要验证用户名。我在想只使用小写 a-z0-9 以避免人们使用类似的用户名?那么密码,我应该在密码中允许哪些字符?
附言。我还是不明白 preg_match 和 ereg。如果有人可以解释这个 "/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+
并为我的用户名进行 preg_match 并密码验证将会非常有帮助。
I am working on validating username, pass and email with php. I need to be sure I get it right so nobody can bypass the login page.
This is the values:
$email=$_POST['email'];
$username=$_POST['uname'];
$passwd=$_POST['pass'];
$passwd2=$_POST['passcopy'];
So far I have email validation:
if(!(preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $_POST['email'])))
die(msg(0,"You haven't provided a valid email"));
password equal:
if ($passwd != $passwd2) {
die(msg(0,"Passwords are not equal"));
}
password length:
if ((strlen($passwd) < 8) || (strlen($passwd) > 16)) {
die(msg(0,"Your password must be between 8 and 16 characters. Please type in a longer password"));
}
I know I need to validate the username. I was thinking only lowercase a-z0-9 to avoid people making similar usernames? Then password, what characters should I allow in a password?
PS. I still don't understand preg_match and ereg. If somebody could explain this "/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+
and make a preg_match for my username and password validation it would be very helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
字符串
^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+
是一个正则表达式,它描述了您验证的模式电子邮件字符串反对。让我为您分解不同的部分。
^
这表示模式应该从行的开头开始匹配。[\.A-z0-9_\-\+]+
这部分由两个子部分组成,第一个[\.A-z0-9_\-\+]
> 描述一类字符,然后是一个 + 表示您需要一个或多个前一类字符。[@]
与一个@
符号完全匹配。[A-z0-9_\-]+
另一类字符,后面带有+
表示您需要该类中的一个或多个字符。the string
^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+
is a regular expression, that describes a pattern that you validate the email string against.Let me break the different parts down for you.
^
this indicates that the pattern should start matching at the beginning of the line.[\.A-z0-9_\-\+]+
this part is composed of two sub parts, first[\.A-z0-9_\-\+]
that describes a class of characters, and then a + that indicates that you want one or more of the previous class.[@]
matches exactly one@
sign.[A-z0-9_\-]+
another class of characters with a+
after that means that you want one or more of the characters that are in the class.字符必须以 ( .a-z0-9_-+ ) 开头的一个或多个先前字符放入 []
该字符不得输入 /// 放入垃圾箱而不输入他
#3# [A-z0- 9_\-]+$// 它应该以 $char
结尾字符串包含 (A-Z0-9_-) 之间的字符 符号加号的平均值是一个或多个先前的字符
$ 是 char 必须以方括号中的前一个字符结尾。
你可以使用与你之前的相同的简单正则表达式。
/^[A-z0-9_\-\+@]+$/
// 我的正则表达式脚本与你之前的相同代码
希望可以帮助你
char have to be start with ( .a-z0-9_-+ ) one or more of previous char into []
this char must not inputed /// put to trash not enter him
#3# [A-z0-9_\-]+$// it should be ending with $char
string contain char between (A-Z0-9_-) mean of sign plus is one or more of previous char
and $ is char have to be end with previous character in square bracket.
u can use simple regex is same as to yours previous..
/^[A-z0-9_\-\+@]+$/
// My regex script do same as your previous code
hope it can help u
是如何检查有效电子邮件地址的一个非常糟糕的例子。尝试谷歌以获得更好的例子。请注意,仅仅因为它匹配 good 正则表达式并不意味着该人正在那里接收邮件 - 如果您需要验证电子邮件地址,更好的方法是将激活 URL 或初始密码邮寄到提供的地址。
erm,正则表达式的课程将花费相当长的时间 - 试试这个
用户名(或密码)包含什么并不重要,只要组合是唯一的并且您处理数据正确
C.
It's a very bad example of how to check for a valid email address. Try google for better examples. Note that just because it matches a good regex does not mean that the person is receiving mail there - a far better approach if you need to validate an email address is to mail an activation URL or an initial password to the supplied address.
erm, a lesson in regexes would take rather a long time - try this
It really doesn't matter what the username (or password) contains as long as the combination is unique and you handle the data properly
C.
preg_match
和ereg
是通过正则表达式。preg
函数使用PCRE(perl兼容正则表达式)引擎,是php推荐的正则表达式函数;ereg
函数现已弃用。我不会详细解释该模式,因为它已在其他答案中介绍,但如果您想要一个真正彻底的正则表达式教程(并且免费,启动),请查看 正则表达式.info 上的教程。
preg_match
andereg
are two ways of matching a string via a regular expression. Thepreg
functions use the PCRE (perl-compatible regular expression) engine and are the recommended regular expression functions for php; theereg
functions are deprecated now.I won't go through explaining the pattern because it's covered in other answers, but if you want a really thorough regular expression tutorial (and free, to boot), check out the tutorial on regular-expressions.info.
电子邮件验证:
http://php.net/manual/en/filter.filters。验证.php
Email validation:
http://php.net/manual/en/filter.filters.validate.php