PHP Cookie 注册和登录表单
PHP Cookies Registration and Login form
本主题的所有读者大家好!我知道这听起来很愚蠢,但我想使用 php 制作一些简单的注册和登录表单。它不适用于网站,因此它不应该是安全的,我不希望名称和密码永远存在。我还想说明我是初学者,我只知道 php 的基础知识。首先,我开始编写 html 登录表单,然后是 php 用于登录以检查是否有任何 cookie。注册是为了以后。
这是代码:
<?php
setcookie($usr,$pwd,time() + (86400 * 30),'/');
setcookie($pwd,$usr,time() + (86400 * 30),'/');
If(isset($_POST['submitForm'])) {
$usr = $_POST['usr'];
$pwd = $_POST['pwd'];
If(isset($_COOKIE[$usr]) && isset($_COOKIE[$pwd])) {
}
else {
die('User has not been registered or wrong username or password.');
}
}
?>
如您所见,我尝试使用 cookie 访问代表 'password' 的 $pwd 和代表 'username' 的 $usr 变量。
这里是 HTML 代码:
<form method="POST" action="logged.php" style="text-align: center;">
<h1>Log In Page of Forum</h1>
<input type="text" name="usr" placeholder="Username">
<br/><br/>
<input type="password" name="pwd" placeholder="Password">
<br/><br/>
<input type="submit" name="submitForm" value="Log in">
<br/>
</form>
是的。 PHP 在文件中。
弹出的错误是:
Notice: Undefined variable: usr in logged.php on line 2
Notice: Undefined variable: pwd in logged.php on line 2
Notice: Undefined variable: pwd in logged.php on line 3
Notice: Undefined variable: usr in logged.php on line 3
但文字仍然出现:
User has not been registered or wrong username or password.
我这样做是为了更好地理解 PHP 而不是认真地使用它。
有人能告诉我我的代码有什么问题吗?我提前谢谢你。 ;)
您的 cookie 调用有误。您应该为 cookie 使用固定名称。您正在使用用户提供的(随机)用户名。
这可行:
setcookie('user', $user, ....);
生成 user=fred
作为 cookie ($_COOKIE['user'] => 'fred'
),而不是
setcookie($usr, $pwd, ...)
你有,这会产生 fred=hunter42
。
因为您不知道用户 return 上的用户名是什么,所以您 NO 知道在 $_COOKIE 中使用什么密钥来检索他们的用户名。 $_COOKIE['ok what is the username?']
?
本主题的所有读者大家好!我知道这听起来很愚蠢,但我想使用 php 制作一些简单的注册和登录表单。它不适用于网站,因此它不应该是安全的,我不希望名称和密码永远存在。我还想说明我是初学者,我只知道 php 的基础知识。首先,我开始编写 html 登录表单,然后是 php 用于登录以检查是否有任何 cookie。注册是为了以后。 这是代码:
<?php
setcookie($usr,$pwd,time() + (86400 * 30),'/');
setcookie($pwd,$usr,time() + (86400 * 30),'/');
If(isset($_POST['submitForm'])) {
$usr = $_POST['usr'];
$pwd = $_POST['pwd'];
If(isset($_COOKIE[$usr]) && isset($_COOKIE[$pwd])) {
}
else {
die('User has not been registered or wrong username or password.');
}
}
?>
如您所见,我尝试使用 cookie 访问代表 'password' 的 $pwd 和代表 'username' 的 $usr 变量。
这里是 HTML 代码:
<form method="POST" action="logged.php" style="text-align: center;">
<h1>Log In Page of Forum</h1>
<input type="text" name="usr" placeholder="Username">
<br/><br/>
<input type="password" name="pwd" placeholder="Password">
<br/><br/>
<input type="submit" name="submitForm" value="Log in">
<br/>
</form>
是的。 PHP 在文件中。
弹出的错误是:
Notice: Undefined variable: usr in logged.php on line 2
Notice: Undefined variable: pwd in logged.php on line 2
Notice: Undefined variable: pwd in logged.php on line 3
Notice: Undefined variable: usr in logged.php on line 3
但文字仍然出现:
User has not been registered or wrong username or password.
我这样做是为了更好地理解 PHP 而不是认真地使用它。
有人能告诉我我的代码有什么问题吗?我提前谢谢你。 ;)
您的 cookie 调用有误。您应该为 cookie 使用固定名称。您正在使用用户提供的(随机)用户名。
这可行:
setcookie('user', $user, ....);
生成 user=fred
作为 cookie ($_COOKIE['user'] => 'fred'
),而不是
setcookie($usr, $pwd, ...)
你有,这会产生 fred=hunter42
。
因为您不知道用户 return 上的用户名是什么,所以您 NO 知道在 $_COOKIE 中使用什么密钥来检索他们的用户名。 $_COOKIE['ok what is the username?']
?