Http authentication, loop cancellation

<?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 use this code that just asks for a username and password for authentication, but the question is when the user clicks the cancel button, he should give an authentication request again and again until the correct username and password are specified.

+4
source share
2 answers

A simple fix can be implemented provided that your clients have JavaScript.

Instead echo 'Text to send if user hits Cancel button';:

echo '<script>window.location.reload();</script>';
+3
source

I think,

header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']); 
exit();

will work in php

0
source

All Articles