0

As far as I can tell from my research, you can't have a multiple-field prompt dialog with any built-in method you can call from JavaScript. So how do the homepages of most routers (192.168.0.1) develop the authentication prompt box, as shown below?

I did a little more research and found that you can setup a basic http authentication using php

$user = 'user';
$password = 'pass';
if (!($_SERVER['PHP_AUTH_USER'] == $user && $_SERVER['PHP_AUTH_PW'] == $password)) {
    header('WWW-Authenticate: Basic realm="Please enter username and password to access this website"');
    header('HTTP/1.0 401 Unauthorized');
    echo '<h1>Unauthorized Access</h1>';
    exit;
}
echo "normal website contents";

1

Jay Ghosh
  • 674
  • 6
  • 24
  • 6
    That box is coming from the browser itself, not JavaScript. It's part of [HTTP basic authentication](https://en.wikipedia.org/wiki/Basic_access_authentication). – Thomas Jul 03 '16 at 14:29

1 Answers1

4

This is a browser feature when the server requires HTTP authentication but none was provided. It's the only exception to the "one field" rule, and you have no JS API to call upon it. (Not to mention it's ugly and the UX is as horrible as alert() or prompt()).

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • Ok. So there aren't any workarounds to implement this on a webpage? – Jay Ghosh Jul 03 '16 at 14:32
  • 1
    @JayGhosh No, and nor should you. Alerts and prompts are system level dialogs that should only be used if you need a decision ***RIGHT NOW OR THE WORLD WILL BURN***, very few things justify holding the user's browser hostage, giving unselectable text and inescapable windows. – Madara's Ghost Jul 03 '16 at 14:34
  • Ok. Thank you. No I wasn't planning on using it for any practical situation. Just curious as to how it works – Jay Ghosh Jul 03 '16 at 14:35
  • @JayGhosh Right, so yeah, it's a browser-level feature, and JS has no access to it. – Madara's Ghost Jul 03 '16 at 14:36