(PHP) Http Authentication
iCodeZ :: iCodeZ :: Tips and Tricks
Page 1 of 1
(PHP) Http Authentication
This will make a username and password box come up, and will not let you past until you type in the correct user/pass.
Here is the entire code:
PHP Code:
$user = 'tim';
$pass = 'bo';
function httpauth(){
header('WWW-Authenticate: Basic realm="My SITE!"');
header('HTTP/1.0 401 Unauthorized');
echo 'Text to send if user hits Cancel button';
exit;
}
while($_SERVER['PHP_AUTH_USER'] != $user && $_SERVER['PHP_AUTH_PW'] != $pass){
httpauth();
}
?>
mypage
Now to explain it all.
PHP Code:
$user = 'tim';
$pass = 'bo';
this defines the username and password. remember to change this.
PHP Code:
function httpauth(){
header('WWW-Authenticate: Basic realm="My SITE!"');
header('HTTP/1.0 401 Unauthorized');
echo 'Text to send if user hits Cancel button';
exit;
}
This function has the HTTP data in it. the header, tells the browser to display a login box. But, if the user presses the Cancel button, it displays the text after the defined headers.
PHP Code:
while($_SERVER['PHP_AUTH_USER'] != $user && $_SERVER['PHP_AUTH_PW'] != $pass){
httpauth();
}
This code is for making sure the user typed in the write username and password. If he didn't type in the write user/pass, the login box comes up again.
HTML Code:
mypage
That is a regular page. After the user has typed in the write user/pass, the rest of the code in the file is executed.
Here is the entire code:
PHP Code:
$user = 'tim';
$pass = 'bo';
function httpauth(){
header('WWW-Authenticate: Basic realm="My SITE!"');
header('HTTP/1.0 401 Unauthorized');
echo 'Text to send if user hits Cancel button';
exit;
}
while($_SERVER['PHP_AUTH_USER'] != $user && $_SERVER['PHP_AUTH_PW'] != $pass){
httpauth();
}
?>
PHP RULES
Now to explain it all.
PHP Code:
$user = 'tim';
$pass = 'bo';
this defines the username and password. remember to change this.
PHP Code:
function httpauth(){
header('WWW-Authenticate: Basic realm="My SITE!"');
header('HTTP/1.0 401 Unauthorized');
echo 'Text to send if user hits Cancel button';
exit;
}
This function has the HTTP data in it. the header, tells the browser to display a login box. But, if the user presses the Cancel button, it displays the text after the defined headers.
PHP Code:
while($_SERVER['PHP_AUTH_USER'] != $user && $_SERVER['PHP_AUTH_PW'] != $pass){
httpauth();
}
This code is for making sure the user typed in the write username and password. If he didn't type in the write user/pass, the login box comes up again.
HTML Code:
PHP RULES
That is a regular page. After the user has typed in the write user/pass, the rest of the code in the file is executed.
iCodeZ :: iCodeZ :: Tips and Tricks
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|