dongpan9760 2016-08-15 21:52
浏览 40
已采纳

PHP Cookies注册和登录表单[重复]

Hi there to all the readers of this topic! I know it sounds foolish, but I want to make some simple registration and login forms using php. It is not for a website so It shouldn't be secure and I don't want the names and passwords to last forever. I also want to specify that I am a beginner and I know just the basics of php. First, I begun to write the html login form, and then the php for the login in order to check if there is any cookie. The registration was for later. Here is the code:

<?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.');
    }
}
?>

As you can see I tried to acess $pwd that stands for 'password' with the $usr variable that stands for 'username', both using the cookies.

Here is the HTML code:

<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>

Yes. The PHP is in the file.

The errors that are popping out are:

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

But the text still appears:

User has not been registered or wrong username or password.

I do this to make a better understanding of PHP and not to use this at something seriously.

Can anybody tell me what is wrong with my code? I thank you in advance. ;)

</div>
  • 写回答

1条回答 默认 最新

  • dongzhi1822 2016-08-15 21:55
    关注

    Your cookie calls are wrong. You should use a FIXED name for the cookies. You're using the (randomish) username the user supplied.

    This would work:

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

    which produces user=fred as a cookie ($_COOKIE['user'] => 'fred'), instead of the

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

    you have, which produces fred=hunter42.

    Since you don't know what the username is on the user's return, you have NO idea what key to use in $_COOKIE to retrieve their username. $_COOKIE['ok what is the username?']?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上