Authenticating Your Users: BY Sana Mateen
Authenticating Your Users: BY Sana Mateen
BY
SANA MATEEN
The HTTP protocol offers a fairly effective means for user authentication, with a typical
authentication scenario proceeding like this:
1. The client requests a restricted resource.
2. The server responds to this request with a 401 (Unauthorized access) response message.
3. The browser recognizes the 401 response and produces a pop-up authentication
prompt . All modern browsers are capable of understanding HTTP authentication
and offering appropriate capabilities, including Internet Explorer, Netscape
Navigator, Mozilla Firefox, and Opera.
4. The user-supplied credentials (typically a username and password) are sent back to the
server for validation. If the user supplies correct credentials, access is granted; otherwise
its denied.
5. If the user is validated, the browser stores the authentication information within its
cache. This cache information remains within the browser until the cache is
cleared, or until another 401 server response is sent to the browser.
Limitation
The header() function sends a raw HTTP header to the browser. The header
parameter specifies the header information sent to the browser. Its prototype follows:
void header(string header [, boolean replace [, int http_response_code]])
The optional replace parameter determines whether this information should replace or
accompany a previously sent header. Finally, the optional http_response_code parameter
defines a specific response code that will accompany the header information.
Applied to user authentication, this function is useful for sending the WWW
authentication header to the browser, causing the pop-up authentication prompt to be
displayed.
It is also useful for sending the 401 header message to the user if incorrect
authentication credentials are submitted.
The isset() function determines whether a variable has been assigned a value. Its
prototype follows:
boolean isset(mixed var [, mixed var [,...]])
It returns TRUE if the variable contains a value and FALSE if it does not.
As applied to user authentication, the isset() function is useful for determining
whether the $_SERVER['PHP_AUTH_USER'] and
$_SERVER['PHP_AUTH_PW'] variables are properly set.
Hard-Coded Authentication
The simplest way to restrict resource access is by hard-coding the username and
password directly into the script.
In the example shown in next slide, if $_SERVER['PHP_AUTH_USER'] and
$_SERVER['PHP_AUTH_PW'] are equal to client and secret, respectively, the
code block will not execute, and anything ensuing that block will execute.
Otherwise, the user is prompted for the username and password until either the
proper information is provided or a 401 Unauthorized message is displayed due to
multiple authentication failures.
Drawbacks:
Foremost, all users requiring access to that resource must use the same
authentication pair
Second, changing the username or password can be done only by entering the
code and making the manual adjustment. The next two methodologies remove
these issues.