[英]Simple PHP Registration Form Only Collects NULL Values?
I am attempting to create a basic login/registration for a website using PHP. 我正在尝试使用PHP为网站创建基本的登录/注册。 The code below shows that I required config.php which I tested and it connects to my site smoothly.
下面的代码显示我需要测试的config.php,它可以顺利连接到我的站点。 What happens is when I go to the page, and enter in values, no matter what I enter for email and password, it says they always match.
发生的是,当我转到页面并输入值时,无论我输入的电子邮件和密码是什么,它都表示它们总是匹配的。 When I try to output the values of email1 and email2, it outputs nothing.
当我尝试输出email1和email2的值时,它什么也不输出。 I think the form isn't collecting the data when you press submit.
我认为按提交时表单未收集数据。 If anyone could see what I am doing wrong it would be so appreciated!
如果有人看到我在做什么错,将不胜感激!
<?php
require('config.php');
if(isset($_POST['submit'])) {
$email1 = $POST['email1'];
$email2 = $POST['email2'];
$pass1 = $POST['pass1'];
$pass2 = $POST['pass2'];
if($email1 == $email2)
{
echo "Emails match.<br />";
if($pass1 == $pass2)
{
echo "Passwords match.<br />";
}
else
{
echo "Sorry, your passwords do not match.<br />";
exit();
}
}
else
{
echo "Sorry, your emails do not match.<br /><br />";
}
}
else {
$form = <<<EOT
<form action="register.php" method="POST">
First Name: <input type="text" name="name" /><br />
Last Name: <input type = "text" name = "lname" /><br />
User Name: <input type = "text" name = "uname" /><br />
Email: <input type = "text" name = "email1" /><br />
Confirm Email: <input type = "text" name = "email2" /><br />
Password: <input type = "password" name = "pass1" /><br />
Confirm Password: <input type = "password" name = "pass2" /><br />
<input type = "submit" value = "Register" name = "submit" />
</form>
EOT;
echo $form;
}
?>
The problem is that you've forgotten the underscores for all $POST
's 问题是您忘记了所有
$POST
的下划线
Change them all to $_POST
将它们全部更改为
$_POST
It's a superglobal which 8 out of 9 of those require the underscore, unlike $GLOBALS
这是一个超级全局变量 ,其中9个变量中的8个需要下划线,与
$GLOBALS
不同
$_POST
<=== Change them all to this. $_POST
<===将它们全部更改为此。 Had error reporting been set/on, you would have been signaled of the error, and multiple times: 如果已设置/打开错误报告 ,则会多次收到信号通知您:
Notice: Undefined variable: POST in...
注意:未定义的变量:POST in ...
To enable it, you can add the following after your opening <?php
tag: 要启用它,可以在打开
<?php
标签之后添加以下内容:
error_reporting(E_ALL);
ini_set('display_errors', 1);
NB: Error reporting should only be done in staging, and never production. 注意:错误报告仅应在登台进行,而不应在生产中进行。
Sidenote: 边注:
Since you're obviously running the entire code from the same page, you can simply change action="register.php"
to action=""
if you want. 由于您显然是在同一页面上运行整个代码,因此您可以根据需要将
action="register.php"
更改为action=""
。
Passwords 密码
I also noticed that you may be storing passwords in plain text. 我还注意到您可能以纯文本形式存储密码。 This is not recommended.
不建议这样做。
Use one of the following: 使用以下之一:
crypt()
bcrypt()
scrypt()
password_hash()
function. password_hash()
函数。 Other links: 其他连结:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.