If HTTP Authentification exists, why custom form to authenticate? - php

If HTTP authentification exists, like this:
if ($_SERVER['PHP_AUTH_USER'] === '...' &&
$_SERVER['PHP_AUTH_PW'] === '...') {
// Process user login.
} else {
header('WWW-Authenticate: Basic realm="Zone");
header('HTTP/1.0 401 Unauthorized');
die("Please enter username and password.");
}
Why google, facebook, everyone is using custom forms to perform authentifications? Am I missing something here?

The PHP_AUTH_USER and PHP_AUTH_PW server variables are by default set when the server is configured with basic authentication and if the client provides credentials through an authentication header.
Sites generally don't use basic authentication because its user experience in browsers is ... horrific.
Using a login form and a POST handler that processes the form, sites can provide a user-friendly, customized login experience.

Related

Double HTTP Authorization

I am currently working on a project that I've decided to go with basic HTTP authorization at the admin area for simplicity, however the company I'm working for already has HTTP authorization on their staging server and I was wondering if it is possible to have double HTTP authorization? Looking at the headers I thought that the realm part is what defines where the user is authorized but if I implement it like that currently, after I enter my credentials for the staging server and then on my inner authorization something that looks like an infinite loop starts, the page never loads.
Is this possible at all or is there some kind of error in my code?
The code is pretty basic stuff:
function require_auth() {
if (!isset($_SESSION['auth'])) {
if ($_SERVER['PHP_AUTH_USER'] === '...' && $_SERVER['PHP_AUTH_PW'] === '...') {
return $_SESSION['auth'] = true;
} else {
header('WWW-Authenticate: Basic realm="uniquerealm"');
header('HTTP/1.0 401 Unauthorized');
}
exit('403 access denied');
}
}
If the HTTP request passes through multiple servers, such as a reverse proxy then an app server, you can use HTTP Basic Auth on each server provided that you accept the same username and password and report the same realm on each server that checks the auth. The realm partitions the URL space that the user sees into different areas, rather than identifying a particular server as I think your question is implying. I've successfully implemented Basic Auth in multiple layers in the past when all 3 pieces matched between servers.
Using python httpx_auth, I successfully passed through two Basic HTTP Authentication with different credentials:
import httpx
from httpx_auth import Basic
with httpx.Client() as client:
a= client.get('https://example.com', auth=Basic('user1', 'password1') + Basic('user2','password2'))

Server-side login prompt box

When I log into my router's firmware, for example, I get a prompt box for the username and password, How is this done? As far as I can tell, JavaScript's prompt() only returns one value.. I know it must be server side because the page continues to "load" until I close the box. I also cannot switch tabs in my browser until the box is closed.
Is it possible to do something like this with PHP? How is this done?
Thanks in advance
its done using Basic HTTP Authentication, for this you need to create a user/password file and tell the server that a folder needs to use the Basic HTTP Authentication. This can be done in the server config or in a .htaccess
Take a look at this page of the PHP docs - HTTP authentication with PHP
Here is the sample code that they give:
<?php
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>";
}
?>
Basically, all you need to do is send the correct headers and the client's browser will be in charge of rendering a native popup with the correct fields.
Once the user has submitted the details your code can validate the login however you want using the $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'] variables.
With Apache as a web server, you could do that with htaccess password protection
The protocol is called http basic authentication

set up HTTP authentication

I need to set up HTTP authentication. I'm lost, I've researched and found the technique and code to validate $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'], and I think I understand that. However I don't understand how to set PHP_AUTH_USER and PHP_AUTH_PW ? I used print-r on $_SERVER and didn't see either? Do I somehow set these in a file somewhere on the server, or do I set using code?
I'm on a shared server hosted by Webfaction.
I realize this might not be a great question, but if someone would point me in the right direction it would be great..
<?php
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>";
}
When the page is called, $_SERVER['PHP_AUTH_USER'] is not set. So the page return header HTTP/1.0 401 Unauthorized that show the modal on your browser.
And when the browser send se second request with ID and password. It send this request:
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
And the super global vars PHP_AUTH_USER and PHP_AUTH_PW are automaticaly setted by PHP.
Sources:
http://en.wikipedia.org/wiki/Basic_access_authentication
http://php.net/manual/en/features.http-auth.php
Here's all the code you need:
$successful = FALSE;
if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW']))
{
$username = $_SERVER['PHP_AUTH_USER'];
$password = $_SERVER['PHP_AUTH_PW'];
if ($username == '-- username --' && $password == '-- password --')
{
$successful = TRUE;
}
}
if ( ! $successful)
{
header('WWW-Authenticate: Basic realm="Secret page"');
header('HTTP/1.0 401 Unauthorized');
}
It would ask for username and password, see if they match and if they don't - ask for them again.
Note that, depending on server configuration, HTTP Basic Authentication may not work.
p.s. You should replace -- username -- and -- password -- with username and password of your own.
To set an environment variable (aka superglobals), the format is:
$_SERVER['variable'] = value (or variable);
http://php.net/manual/en/language.variables.superglobals.php - good info on PHP superglobal variables.

Basic Authentication URL Handling

We have a new b2b vendor that wants to use basic auth via URL.
They want to authenticate like this:
//URL coming into our server
http://usernametext:passwordtext#our.company.com/listener.php
How can I get the username and password from the URL via my listener.php script?
I have tried setting basic auth headers per the php man page but it pops up a login box, which is not what I need, since these are web services talking to each other, not people:
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo '<response><error>No username and password found</error></response>';
exit;
} else {
//process request if username & password are legit
}
Thanks to all that offered solutions. Here is what solved this issue for me:
It was a server configuration issue. I needed to compile the Apache extension in my PHP. Once I did that, the $_SERVER array contained $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'] values. Prior to this, those values were missing from the $_SERVER array
Why not send the info via good old $_GET? Something like
http://our.company.com/listener.php?usr='me'&pswd='secret'
may be this could help:
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>";
}
more detailed you can check on : http-auth
Update: If you dont need popup box (which is confirmation user+pass on non required auth site) simply add .htaccess on directory.
AuthType Basic
AuthName "Password Required"

PHP user authentication like a router login

how to create user authentication in php just the same way when we try to login to a router.
when i enter the url for example www.example.com/portal there should be a prompt like the above image asking username and password.
what type of authentication is this. how to code that in php.
NOTE: i have to full control of the server that i run. so is there any special module that needs to be installed i can do that.
This is called Basic Auth. See this example from the documentation:
<?php
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>";
}
?>
http://php.net/manual/en/features.http-auth.php
Essentially, you send the right headers with the status code of 401 Unauthorized. the browser sees this along with your WWW-Authenticate header and prompts the user for you. Once this is done, you are able to see the username and password in $_SERVER['PHP_AUTH_USER'] as well as $_SERVER['PHP_AUTH_PW'].
You should know though that if you are using basic auth, the username/password are sent plaint-text. You must use HTTPS if you want any sort of security. Also, depending on your application, you will see that there is no way to effectively "log out". Most browsers remember the username/password for the entire session, and send it with every subsequent request.
This is basic http authentification. You could find a tutorial on the php.net page: http://www.php.net/manual/en/features.http-auth.php
It is basically a http header, that you have to send to the browser. The http server (apache/nginx) will forward the userdata afterwards to php like any other $_SERVER parameter.

Categories