使用 php 验证用户名、密码和电子邮件

发布于 2024-09-13 08:35:22 字数 916 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

黄昏下泛黄的笔记 2024-09-20 08:35:23

字符串 ^[\.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.

寻梦旅人 2024-09-20 08:35:23
"/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+/"// write same checking on same line

#1# ^[\.A-z0-9_\-\+]+

字符必须以 ( .a-z0-9_-+ ) 开头的一个或多个先前字符放入 []

#2# [@]

该字符不得输入 /// 放入垃圾箱而不输入他

#3# [A-z0- 9_\-]+$// 它应该以 $char 结尾

字符串包含 (A-Z0-9_-) 之间的字符 符号加号的平均值是一个或多个先前的字符
$ 是 char 必须以方括号中的前一个字符结尾。

你可以使用与你之前的相同的简单正则表达式。

/^[A-z0-9_\-\+@]+$/ // 我的正则表达式脚本与你之前的相同代码

希望可以帮助你

"/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+/"// write same checking on same line

#1# ^[\.A-z0-9_\-\+]+

char have to be start with ( .a-z0-9_-+ ) one or more of previous char into []

#2# [@]

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

PS。我还是不明白 preg_match 和 ereg。如果有人能解释一下

是如何检查有效电子邮件地址的一个非常糟糕的例子。尝试谷歌以获得更好的例子。请注意,仅仅因为它匹配 good 正则表达式并不意味着该人正在那里接收邮件 - 如果您需要验证电子邮件地址,更好的方法是将激活 URL 或初始密码邮寄到提供的地址。

为我的用户名和密码验证进行 preg_match

erm,正则表达式的课程将花费相当长的时间 - 试试这个

用户名(或密码)包含什么并不重要,只要组合是唯一的并且您处理数据正确

C.

PS. I still don't understand preg_match and ereg. If somebody could explain this

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.

make a preg_match for my username and password validation

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.

弥枳 2024-09-20 08:35:23

preg_matchereg 是通过正则表达式preg函数使用PCRE(perl兼容正则表达式)引擎,是php推荐的正则表达式函数; ereg 函数现已弃用。

我不会详细解释该模式,因为它已在其他答案中介绍,但如果您想要一个真正彻底的正则表达式教程(并且免费,启动),请查看 正则表达式.info 上的教程

preg_match and ereg are two ways of matching a string via a regular expression. The preg functions use the PCRE (perl-compatible regular expression) engine and are the recommended regular expression functions for php; the ereg 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.

思念满溢 2024-09-20 08:35:23

电子邮件验证:

if (filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)  == TRUE) {
    $email = $_POST["email"];
} else {
    echo "email error";
    die;
}

http://php.net/manual/en/filter.filters。验证.php

Email validation:

if (filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)  == TRUE) {
    $email = $_POST["email"];
} else {
    echo "email error";
    die;
}

http://php.net/manual/en/filter.filters.validate.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文