简体   繁体   English

php:使用cookies / user authentification

[英]php: using cookies / user authentification

i'm looking for the optimal way for safe user authentification to my website. 我正在寻找安全用户身份验证到我的网站的最佳方式。

i'm thinking about the following solution: 我正在考虑以下解决方案:

first login: asking for login data -> generating hashcode consisting of ip address, password and cookie expiration date -> storing that hashcode to database but also into a cookie. 首次登录:要求登录数据 - >生成由ip地址,密码和cookie到期日期组成的哈希码 - >将该哈希码存储到数据库,但也存储到cookie中。

next login: check for cookie, look for hashcode in my user database. 下次登录:检查cookie,在我的用户数据库中查找哈希码。

would that be the safest way? 那会是最安全的方式吗? or will there be problems using cookies? 或者使用cookies会有问题吗? thanks 谢谢

Generally, you will want to store a cookie in the browser that points to a record in your database somewhere that says that he is a known user. 通常,您需要在浏览器中存储一个cookie,该cookie指向数据库中某条记录表明他是已知用户的记录。 The one thing you'll want to ensure is that that cookie id is globally unique. 您要确保的一件事是该cookie ID是全局唯一的。 So, yes, using a hash (or md5, or sha1) on a variety of unique attributes would most likely be sufficient. 所以,是的,在各种独特属性上使用散列(或md5或sha1)很可能就足够了。 Putting a unique on that column in your database would be a good idea as well. 在数据库中为该列添加unique也是一个好主意。

Another idea would be to have a column in your users table for cookieid , generate that when the entry is created and ensure it's uniqueness. 另一个想法是在您的users表中为cookieid创建一个列,在创建条目时生成该列并确保它的唯一性。 Then just use that every time when that user logs in. You could change all the values of that column for every user with a backend script every once in a while if you want to freshen up the cookies if you're really worried about security. 然后每次用户登录时都使用它。如果您真的担心安全性,如果您想要刷新cookie,可以每隔一段时间为每个用户更改该列的所有值。

will there be problems using cookies?

Except for newer html5 features that aren't compatible with all browsers, cookies are really the only way to save login information. 除了与所有浏览器不兼容的较新的html5功能外,cookie实际上是保存登录信息的唯一方法。 Go to a big site you use, ebay, amazon, wellsfargo. 去你使用的大网站,ebay,amazon,wellsfargo。 Wipe all of your cookies and private data, then go there and login. 擦除所有cookie和私人数据,然后去那里登录。 Then view your cookies. 然后查看您的cookie。 What do they put in there? 他们放在那里什么? If it's good enough for those guys, it's probably good enough for you. 如果它对那些家伙来说足够好,那对你来说可能已经足够了。

The best and easiest way is to use php-sessions. 最好和最简单的方法是使用php-sessions。

<?php
session_start();
$_SESSION["user"] = "foo";
$_SESSION["ip"] = "123.132.123.132";
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM