简体   繁体   English

PHP的HTTP身份验证到底如何工作?

[英]How exactly does HTTP Authentication with PHP work?

I really don't get it. 我真的不明白。 I don't understand how .htaccess, php and HTTP work together within the topic http-authentication. 我不了解.htaccess,php和HTTP在主题http-authentication中如何协同工作。

how can this be achieved: I have a folder where i want to prevent access to unauthorized people. 如何做到这一点:我有一个文件夹,希望防止未经授权的人访问。 in this folder i have images, for example, and if the user is authorized, the image should be displayed, if not he should get the possibility to enter a username and password for this one request. 例如,在此文件夹中,我有图像,并且如果用户被授权,则应显示该图像,如果没有,则应该为该请求输入用户名和密码。

do i need an .htaccess file, that redirects to a php file, that checks and handles the authentication and sends appropriate headers and also outputs the requested file? 我需要一个.htaccess文件,该文件重定向到php文件,该文件检查并处理身份验证并发送适当的标头并输出所请求的文件吗?

or must i do something else? 还是我必须做别的事情? am i to solve this completely different? 我要解决这个完全不同的问题吗?

  • You configure the server to perform authentication before passing the request on to the PHP or 您将服务器配置为执行身份验证,然后再将请求传递给PHP
  • You write PHP that will check the submitted credentials and return suitable HTTP headers to request them if they are missing or wrong 您编写的PHP将检查提交的凭据,并返回合适的HTTP标头以请求它们是否丢失或错误。

do i need an .htaccess file 我需要一个.htaccess文件吗

Only if you use the first of the above approaches. 仅当您使用上述方法中的第一种时。 Even then, you are usually better off putting the configuration in your main server config (it is more efficient). 即使那样,通常也最好将配置放在主服务器配置中(效率更高)。

that redirects to a php file 重定向到一个PHP文件

If you go with the second approach, you would generally include your authentication code at the top of each page that needs authenticating. 如果采用第二种方法,通常会在每个需要认证的页面的顶部include认证代码。 (For a simple approach anyway, MVC approaches tend to have a more modular means of doing things). (无论如何,对于一种简单的方法,MVC方法往往具有一种更加模块化的服务方式)。

You have a few options. 您有几种选择。

1 - Basic Authentication - When a user does a request, apache checks for htaccess file and when basic auth is set, it returns a header for authentication (when no login info has been sent). 1- 基本身份验证 -当用户发出请求时,apache将检查htaccess文件,并且在设置基本身份验证后,它将返回用于身份验证的标头(未发送登录信息时)。 The webbrowser reacts on that and gives a native login screen. Web浏览器对此做出反应并提供一个本机登录屏幕。 This screen is very well supported by all browsers and password remember tools. 所有浏览器和密码记住工具都很好地支持此屏幕。 When entering the credentials the next requests sent the credentials (unencrypted) each time, so apache doesn't gives the login screen each time. 输入凭据时,下一个请求每次都会发送凭据(未加密),因此apache不会每次都显示登录屏幕。 You can read more on it here 你可以在这里阅读更多

How to set it up, create .htaccess file: 如何进行设置,创建.htaccess文件:

AuthName "Protected"
AuthType Basic
AuthUserFile securepath/.htpasswd
Require user authuser

Create the .htpasswd at the command line: 在命令行中创建.htpasswd:

$ adduser authuser
$ passwd authuser
$ htpasswd  -c securepath/.htpasswd  authuser

But many control panels have tools to set this up using an interface. 但是许多控制面板都有工具可以使用界面进行设置。

2 - Own system - You could write your own auth system where in your code you validate whether a user is authorised or not. 2-自己的系统-您可以编写自己的身份验证系统,在该系统中,您可以在代码中验证用户是否被授权。 You can build a login screen where the user when granted gets a cookie that represents the user on your server. 您可以构建一个登录屏幕,在该屏幕上,被授予用户权限的用户将获得一个代表服务器上用户的cookie。 But the cookie itself is unencrypted and can be read by others. 但是cookie本身未加密,并且可以被其他人读取。

The latter gives you the option to the user to have different passwords more easier. 后者使您可以选择让用户更轻松地使用不同的密码。 And the password isn't sent each time, only the cookie. 而且密码不是每次都发送,只有cookie。

For security I advise using SSL/HTTPS connection where everything is encrypted. 为了安全起见,我建议使用SSL / HTTPS连接,其中的所有内容均已加密。

You can restrict access to entire folders using Basic Authentication (which is probably the easist to set up). 您可以使用“基本身份验证”(可能很容易设置)来限制对整个文件夹的访问。 It's fairly straight forward. 这很简单。

The first thing you need is your .htaccess file. 您需要的第一件事是您的.htaccess文件。 This file needs to reside inside of the folder that you want to restrict access to. 该文件需要驻留在您要限制访问的文件夹内。 For example /restrict/.htaccess 例如/restrict/.htaccess

This .htaccess file needs to have 该.htaccess文件需要具有

#set up basic authentication
AuthType Basic
#provide password verification file
AuthUserFile /PATH_TO_HTPASSWD_FILE/.htpasswd
#set user names as "require user *******"
require user my_username

Then in your .htpasswd file you verify your username/password like so: 然后在您的.htpasswd文件中,按如下所示验证您的用户名/密码:

my_username:$1$jeTmJQpY$gKrWlJqL6dCCSX62Hspfp0

Verification is done by "username:password" << colon separation, and the password can be stored in various ways (crypt,plain text, sha1, md5). 验证通过“ username:password” <<冒号分隔完成,并且密码可以以多种方式存储(crypt,纯文本,sha1,md5)。 In the above example i chose the password "password" and used crypt as the output. 在上面的示例中,我选择了密码“ password”,并使用crypt作为输出。

It is highly recommended to not store the .htpasswd file inside of the same directory. 强烈建议不要将.htpasswd文件存储在同一目录中。

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

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