简体   繁体   English

如何在本地cookie中存储身份验证详细信息

[英]How to store Authentication Details in in local cookies

I have a website url like www.example.co.in . 我有一个网站网址,如www.example.co.in

I upload a webpage reservation.php to the above website using FTP, but whenever I open the page www.example.co.in/reservation.php in a browser, I need to give the FTP username and password in the authentication Messagebox. 我上传网页reservation.php使用FTP上述网站,但每当我在浏览器中打开网页www.example.co.in/reservation.php,我需要给在认证消息框的FTP用户名和密码。

I want to declare that username and password in a session or cookie and whenever I open the link www.example.co.in/reservation.php in a browser, that stored session values automatically fill the box and redirect to the page without asking for the username and password. 我想在会话或cookie中声明该用户名和密码,并且每当我在浏览器中打开链接www.example.co.in/reservation.php时,存储的会话值都会自动填充该框并重定向到页面而无需询问用户名和密码。

When the browser closes,the stored session has to be destroyed. 当浏览器关闭时,必须销毁存储的会话。

A cookie is basically just a item in a dictionary. Cookie基本上只是字典中的一项。 Each item has a key and a value. 每个项目都有一个键和一个值。 For authentication, the key could be something like 'username' and the value would be the username. 对于身份验证,密钥可以是“用户名”之类的值,而值将是用户名。 Each time you make a request to a website, your browser will include the cookies in the request, and the host server will check the cookies. 每次您向网站提出请求时,浏览器都会在请求中包含cookie,并且主机服务器将检查cookie。 So authentication can be done automatically like that. 因此,可以像这样自动完成身份验证。

To set a cookie, you just have to add it to the response the server sends back after requests. 要设置cookie,只需将其添加到服务器在请求后发回的响应中。 The browser will then add the cookie upon receiving the response. 然后,浏览器将在收到响应后添加cookie。

There are different options you can configure for the cookie server side, like expiration times or encryption. 您可以为Cookie服务器端配置不同的选项,例如到期时间或加密。 An encrypted cookie is often referred to as a signed cookie. 加密的cookie通常称为签名的cookie。 Basically the server encrypts the key and value in the dictionary item, so only the server can make use of the information. 基本上,服务器会加密字典项中的键和值,因此只有服务器可以使用该信息。 So then cookie would be secure. 因此,cookie将很安全。

A browser will save the cookies set by the server. 浏览器将保存服务器设置的cookie。 In the HTTP header of every request the browser makes to that server, it will add the cookies. 在浏览器对该服务器发出的每个请求的HTTP标头中,它将添加cookie。 It will only add cookies for the domains that set them. 它只会为设置它们的域添加cookie。 Example.com can set a cookie and also add options in the HTTP header for the browsers to send the cookie back to subdomains, like sub.example.com. Example.com可以设置cookie,还可以在HTTP标头中添加选项,以供浏览器将cookie发送回子域,例如sub.example.com。 It would be unacceptable for a browser to ever send cookies to a different domain. 浏览器曾经将cookie发送到其他域是不可接受的。

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

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