iCodeZ
Would you like to react to this message? Create an account in a few clicks or log in to continue.

(PHP) Http Authentication

Go down

(PHP) Http Authentication Empty (PHP) Http Authentication

Post by {#LAG#} Insane Wed Jun 04, 2008 9:04 pm

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


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:


mypage


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.

What a Face

{#LAG#} Insane
Admin
Admin

Number of posts : 29
Age : 27
Location : Your Mums WoW Server.
Registration date : 2008-06-04

http://www.icodez.co.nr

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum