By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,949 Members | 1,869 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

Login, authentication: After entering username+psw nothing happens?

P: 4
I have created a blog and have added a login box which prompts the user for login and id before posting- The username and password have been stored in the database, however when i enter the username and pasword it does not seem to compare the values entered with anything. It jus keeps giving the prompt box to enter details. But if i click cancel, then it tels me the incorrect credentials ave been entered i have tried many ways to solve this problem but cant seem to see where i am gng wrong- the code is below, and some of the code was already provided, Function to authenticate was given -

And this code is called when the user tries to enter a new posting option from the index page, so before the addentry page displays, the user must login- if any more code is required, let me know-

Thanks in advance- This is my first ever php program and it was all going ok untill this. thanks

[PHP] // Function to authenticate
function authenticated($username, $password) {

// Connect to Database Server

$Hostname = "2006";
$Username = "anon";
$Password = "anonpassword";

//open the connection

$conn = mysql_connect($Hostname,$Username, $Password);

// Choose database

mysql_select_db("anon", $conn);

// Build SQL query to:
// find row in database with
// matching $username and $password
// Execute query

$query = mysql_query( "SELECT * FROM blog_user WHERE name == '".$username."' AND password == '".$password."';", $conn);


// IF number of rows is equal to one
// return true

if ($query == 1)
{
return true;
}

// ELSE return false
else
{
return false;
}

}


// Assign username and password from $_SERVER global array

$username = $_SERVER["PHP_AUTH_USER"];
$password = $_SERVER["PHP_AUTH_PW"];



// Decide whether to show blog entry form, or deny access
if (!authenticated($username, $password)) {

// Credentials either:
// not sent (1st time script is called)
// credentials do not match database

// Display HTTP Authentication Challenge

header("WWW-Authenticate: Basic Realm=\"Blog Station\"");
header("HTTP/1.1 401 Unauthorised");

// Print a message about incorrect credentials

echo " Incorrect credentials provided, please try again";
echo ("<a href = \"index.php\"> Re-Enter");
exit;
}

else {
// Correct credentials provided, print the blog entry form
//using the echo as a check
echo " Authorised";
}[/PHP]
Feb 16 '07 #1
Share this Question
Share on Google+
5 Replies


Motoma
Expert 2.5K+
P: 3,235
Your MySQL query should use '=' rather than '=='

Where is the code that is getting the username and password? Shouldn't these be taken from a login form? I doubt the $_SERVER[] superglobal will have the correct value.
Feb 16 '07 #2

P: 4
thanks for replying,

The = does not fix the problem, it still does the same thing -

Sorry i dont quite understand what you mean by where the code is for username and password. The username and password have been entrered into the table blog_user, i inserted one valid user into the database.

as for the $_SERVER[] superglobal it was given to us like that, what would be the alternative way of implementing it ?
Feb 16 '07 #3

P: 4
anyone, able to help please?
Feb 18 '07 #4

ronverdonk
Expert 2.5K+
P: 4,258
From your reply to motoma I deduct that you are using code that you got from somewhere else. And you have no clue as how to implement and use it.

Anyway it is not going to work as you showed it. And what you showed is only part of a bigger script.

So you either display that script or you start to develop a login routine yourself. We can help you with the latter,

Ronald :cool:
Feb 18 '07 #5

P: 4
From your reply to motoma I deduct that you are using code that you got from somewhere else. And you have no clue as how to implement and use it.

Anyway it is not going to work as you showed it. And what you showed is only part of a bigger script.

So you either display that script or you start to develop a login routine yourself. We can help you with the latter,

Ronald :cool:
Just to clarify i did not copy the code from someone esle or take someone elses code, yes it was given to us by our lecturers, as most people taking the class have little or no knowledge of php and we are only required to write some of the code- Anwyay thanks for your help. I will ask someone esle.
Feb 19 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.