dongsou4301 2014-08-31 10:51
浏览 9
已采纳

如何在我的网站上进行安全登录?

Good day. I'm writing a social network and I need to make a very protected auth form. I decided to post data through ajax and save a login into cookies. BUT login can be wrote in hacker's cookies easily. Then I remembered about user hash, which can be generated from PHP every time when user logins and write in DB and then compare. BUT login must be allowed in different devices at the same time. How to make a secure auth?

  • 写回答

1条回答 默认 最新

  • dongposhi8677 2014-08-31 11:08
    关注

    If you want a secure login, you have to think about https.

    I would use the PHP super global $_SESSION to store your session status, user email etc.

    Example using PDO in PHP:

    function login($email, $password)
    {
        $stmt = $pdo->prepare("SELECT password FROM users WHERE email = ? LIMIT 1");
        $stmt->execute(array($email));
        $pw = $stmt->fetchColumn();
        if(password_verify($password, $pw)) {  //password is correct
            $_SESSION['logged_in'] = true;
            $_SESSION['email'] = $email;
            return true;
        } else return false;
    }
    

    You should definetely use prepared statements since you take user input that might be dangerous. And don't forget about the password_hash and password_verify functions, so the passwords are encrypted safely. Store the encrypted passwords in your database and check if the encrypted user input mathes the encrypted password. DON'T USE md5, it's not safe.

    Follow this link to learn more about $_SESSION 's

    Conclusion

    • use $_SESSIONs
    • password_hash and password_verify instead of md5
    • use prepared statements when querying your database

    Hope this helps a little bit.

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

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么