简体   繁体   中英

HTTP Authentication PHP

<?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>";
}
?>

I got this code from php.net

is it possible to set target folder(or URL), userID and password? I don't want to show popup login window to user

for example)

after line of header('HTTP/1.0 401 Unauthorized');

header('URL:./target_folder/')
header('UserID:guest')
header('Password:guest')

I'm not sure about what you want, but you can pass your username and password in the URL like this :

https://username:password@www.example.com/path

It will not display any login popup.

Doc : https://developer.mozilla.org/en-US/docs/Web/HTTP/Basic_access_authentication

Be aware of the Security section

The BA mechanism provides no confidentiality protection for the transmitted credentials. They are merely encoded with BASE64 in transit, but not encrypted or hashed in any way. Basic Authentication is, therefore, typically used over HTTPS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM