0

嗨,这个主题的所有读者!我知道这听起来很愚蠢,但我想使用 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.');
    }
}
?>

如您所见,我尝试使用代表“用户名”的 $usr 变量访问代表“密码”的 $pwd,两者都使用 cookie。

这是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,而不是认真地使用它。

谁能告诉我我的代码有什么问题?我提前谢谢你。;)

4

1 回答 1

0

您的 cookie 调用是错误的。您应该为 cookie 使用固定名称。您正在使用用户提供的(随机)用户名。

这会起作用:

setcookie('user', $user, ....);

user=fred作为 cookie ( $_COOKIE['user'] => 'fred') 生成,而不是

setcookie($usr, $pwd, ...)

你有,它产生fred=hunter42.

由于您不知道用户返回的用户名是什么,因此您不知道在 $ _COOKIE中使用什么键来检索他们的用户名。$_COOKIE['ok what is the username?']?

于 2016-08-15T21:55:42.757 回答