Gary Mack
Gary Mack

Reputation: 11

HTTP Authentication won't work on localhost Zend WAMP server

I've been working through PHP, MySQL, Javascript & CSS 2nd edition by Robert Nixon (O'Reilly 2012). I've managed to set up a database on my WAMP server (Zend server).

The book then shows how to authenticate a username and password. i managed to create a few usernames with passwords which have been added to my database successfully. I know because I connected to the database, read the result of a SELECT query and echo 'd them out to the browser.

When I then try to login with authentication here:

if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {....perform database query and compare username/password....}

none of my usernames and passwords work. The window 'Authentication Required' pops up again in both Firefox and IE. The PHP code isn't getting past the if statement (I've tried sending output to the browser immediately before and after the if statement) so it seems that the username and password entered are not being passed to the PHP server.

I made sure that 'localhost' is authorized specifically on Firefox, but still no success.

Any suggestions would be welcome.

Gary

Upvotes: 0

Views: 1950

Answers (1)

Giuliano Lancioni
Giuliano Lancioni

Reputation: 11

Found! I had the same problem while trying the same example (!) and discovered that the problem arises when php is run as cgi. You can detect it by calling php_sapi_name(): if the function returns 'cgi-fcgi' (which seems to be the default under Zend), basic authentication doesn't work. You find the same information in phpInfo under Server API (CGI/FastCGI).

In my case too, dev version has this problem while live version has not (the website uses Apache 2.0 Handler). By now, I just added a check that disables authentication if cgi-fgci is detected (I don't need authentication on localhost), but of course I'd like to find a viable alternative.

Upvotes: 1

Related Questions