Intekhab Khan
Intekhab Khan

Reputation: 1863

How to do server side authentication in php

Server authentication

How to do above authentication in PHP.I already have my own authentication in php but how to do above type login screen.

Upvotes: 1

Views: 3348

Answers (3)

DavidS
DavidS

Reputation: 1690

Instead of using Apache, you can do this in PHP: http://php.net/manual/en/features.http-auth.php

Basically, you have the username and password available in $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'], which you can use to lookup users in a database or whatever.

A basic example, from php.net:

if (!isset($_SERVER['PHP_AUTH_USER'])) {
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Text to send if user hits Cancel button';
    exit;
} else {
    echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
    echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
}    

Upvotes: 4

Grim...
Grim...

Reputation: 16953

That authentication is controlled by Apache, not PHP.

Take a look at this tutorial: http://www.seochat.com/c/a/Search-Engine-Optimization-Help/Apache-Basic-User-Authentication-htpasswd-Tutorial-for-SEO/

Upvotes: -1

Related Questions