PHP user authentication as router login

how to create user authentication in php exactly the same when we try to enter the router.

enter image description here

when I enter the URL, for example, www.example.com/portal, there should be a prompt similar to the above image with the username and password.

what type of authentication. how to encode this in php.

NOTE. I have to completely control the server that I am running. therefore there is a special module that needs to be installed, I can do it.

+4
source share
3 answers

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 are sending the correct headers with a status code of 401 Unauthorized. the browser sees this along with your WWW-Authenticate header and asks the user for you. Once this is done, you can see the username and password in $_SERVER['PHP_AUTH_USER'] , as well as $_SERVER['PHP_AUTH_PW'] .

You should know that if you use basic auth, the username / password is sent in paid text. You must use https if you want any security. In addition, 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 each subsequent request.

+3
source

This is basic http authentication. You can find the tutorial at php.net: http://www.php.net/manual/en/features.http-auth.php

This is basically an http header that you should send to your browser. The HTTP server (apache / nginx) will redirect user data after that to php, like any other $ _SERVER parameter.

0
source

Check out this article: http://en.wikipedia.org/wiki/.htaccess This actually has nothing to do with PHP.

You will need two files: .htaccess and .htpasswd, which you can create here: http://www.webmaster-toolkit.com/htaccess-generator.shtml

-1
source

All Articles