0

I have to prompt an authentication for a file while opening on every time. I have tried it with PHP Authentication.

if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) {
}

But, It is not asking for every time. Can we do it either using Htaccess or Htpasswd?

Please advice.

user2987836
  • 1,543
  • 2
  • 10
  • 8
  • If it is not asking every time it would imply that one of the conditions is being met, try changing it from `||` to `&&` – Pwner Jan 24 '14 at 12:00
  • `||` means OR. Try `&&` which means AND. – Realitätsverlust Jan 24 '14 at 12:01
  • @YUNOWORK & Pwner: It should be authenticated every time. – user2987836 Jan 24 '14 at 12:07
  • That’s not how HTTP Auth works – your browser remembers the credentials and automatically sends them with every following request. (Otherwise, on a page with resources like scripts, stylesheets, images etc. embedded that are also within the same realm, you would be prompted again for each and every one of those, and that can obviously not be the desired effect for the normal use case.) – CBroe Jan 24 '14 at 12:19
  • Although you can kinda “clear” the browsers memory [by answering a request with a 401 Unauthorized](http://www.php.net/manual/en/features.http-auth.php#example-353), for your case you would have to find a way of checking whether or not the client requested that resource before, so that you know _when_ to send that header – I’m not sure if there’s a trivial solution for that. – CBroe Jan 24 '14 at 12:19

1 Answers1

0

You can try using .htaccess & .htpasswd

create file .htaccess & put :

AuthType Basic
AuthName "restricted area"
AuthUserFile /opt/lampp/htdocs/test/.htpasswd  # should be you file path & .htpasswd
require valid-user

the above solution for whole directory. if you want for single file than use this in .htaccess

AuthType Basic
AuthName "demo.php" # name of the file for which you want authentication
AuthUserFile /opt/lampp/htdocs/test/.htpasswd #path of the folder
<Files "demo.php">

  require valid-user

</Files>

in .htpasswd

test:do43GnYbHanDE #username test & pwd (download)

You can create your .htpasswd password from here

Harshal
  • 3,562
  • 9
  • 36
  • 65