PHP and .htaccess authentication solution

后端 未结 1 1002
野趣味
野趣味 2021-02-06 13:29

Here\'s the layout:

web root
  - admin (dir)
      - index.php
      - js
      - img
      - other files / dirs
  - dir
  - files

Until now, I

相关标签:
1条回答
  • 2021-02-06 14:28

    You can use the SetEnvIf variable in the .htaccess file to check if a certain Cookie value is set. For example (this isn't very secure, but just for illustration):

    AuthType Basic
    AuthName "Protected Login"
    AuthUserFile "/path/to/.htpasswd"
    AuthGroupFile "/dev/null"
    SetEnvIf Cookie PHPSESSID=.* PASS=1
    Order deny,allow
    Deny from all
    Allow from env=PASS
    Require valid-user
    Satisfy any
    

    The line SetEnvIf Cookie PHPSESSID=.* PASS=1 checks if a Cookie is set with a PHP session id and if so, that is enough to Satisfy the authentication process and the Allow from env=PASS makes it skip the login prompt if this is true.

    Again, this example is not very safe as a PHP session cookie is already set when session_start() is called without a succesful authentication attempt, so it would be better to set a more cryptical/random cookie value that's hard to guess. For example:

    SetEnvIf Cookie AJNC3Z921dmc4O8P2 PASS=1
    

    That way, if you set a cookie value of AJNC3Z921dmc4O8P2 upon succesful authentication through PHP, this will be enough to pass the authentication process. Make sure to set a proper cookie expiration time though to avoid people from being able to pass the login prompt for a prolonged period.

    0 讨论(0)
提交回复
热议问题