Jump to content

Recommended Posts

hey All, i have cheked out about 4tutorials on Http authentication including the one on php freaks, which was the first.

i am having some problems with it,

being that i put the username and password in and all it does is reopen the password box.

i did think that maybe my host doesnt allow this, if that is possible, but then i thought again that it would display the losing box.

here is my code.

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
<?php

$auth = false; // Assume user is not authenticated

if (isset( $PHP_AUTH_USER ) && isset($PHP_AUTH_PW)) {

// Connect to MySQL

mysql_connect( 'db1.awardspace.co.uk', 'tkd1987_aaron', 'raymond' )
or die ( 'Unable to connect to server.' );

// Select database on MySQL server

mysql_select_db( 'tkd1987_aaron' )
or die ( 'Unable to select database.' );

// Formulate the query

$sql = "SELECT * FROM users WHERE
username = '$PHP_AUTH_USER' AND
password = '$PHP_AUTH_PW'";

// Execute the query and put results in $result

$result = mysql_query( $sql )
or die ( 'Unable to execute query.' );

// Get number of rows in $result.

$num = mysql_numrows( $result );

if ( $num != 0 ) {

// A matching row was found - the user is authenticated.

$auth = true;

}

}

if ( ! $auth ) {

header( 'WWW-Authenticate: Basic realm="Private"' );
header( 'HTTP/1.0 401 Unauthorized' );
echo 'Authorization Required.';
exit;

} else {

echo '<P>You are authorized!</P>';
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
<meta name="generator" content="Adobe GoLive">
<title>Untitled Page</title>
</head>

<body bgcolor="#ffffff">
<p>Login Successful:</p>
<p></p>
<p></p>
</body>

</html>[/quote]
Link to comment
https://forums.phpfreaks.com/topic/5957-http-authenication/
Share on other sites

HTTP Basic authentication only will work if Apache is running as a module.

Those variables will only work if you have register_globals on. It's best to keep it off and use $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'] instead. See:

[a href=\"http://us3.php.net/manual/en/features.http-auth.php\" target=\"_blank\"]http://us3.php.net/manual/en/features.http-auth.php[/a]

How did you set the passwords to begin with?
Link to comment
https://forums.phpfreaks.com/topic/5957-http-authenication/#findComment-21325
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.