Forum Moderators: coopster

Message Too Old, No Replies

HTTP Auth Variable problem

SQL query wont execute

         

Jonramshaw

12:56 pm on Sep 3, 2009 (gmt 0)

10+ Year Member



Okay going out of my mind here. I have use a different version of this before that worked so I wanted to adopt it. But basically the SQL query wont execute.

I am hoping someone can help. Code is as follows. (Btw i know its basic its just a proof of concept) Many Thanks

<?php require_once('../../../Connections/test.php');

$auth = false; // Assume user is not authenticated
$name =$_SERVER['PHP_AUTH_USER'];
$pass = $_SERVER['PHP_AUTH_PW'];
if (isset( $name ) && isset($pass)) {

// the query

$query = "SELECT name, password FROM Admin WHERE
Name = '".$name."' AND
Password = '".$pass."'";

// Execute the query and put results in $result

$result = mysql_query( $query )
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="Pharmacy Admin"' );
header( 'HTTP/1.0 401 Unauthorized' );
echo "Authorization Required";

} else {

session_register('authorised');
header("location:sucess.php?title=go");


}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Login</title>
<link href="../../../css/1.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="header3">
The details you entered are in correct <a href="../../../index.html">click here</a> to return to the home page</div>
</body>
</html>

andrewsmd

9:19 pm on Sep 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this
$query = "SELECT name, password FROM Admin WHERE
Name = '".$name."' AND
Password = '".$pass.";'"; //added a ;

Note: the best way to debug sql queries is to echo them on the page and then paste them in a mysql shell and see what error it gives you.

Jonramshaw

9:30 am on Sep 4, 2009 (gmt 0)

10+ Year Member



nope it still says unable to execute query .. i will try echo'ing the query and see what the sql shell gives me , thanks andrew :)

Jonramshaw

1:47 pm on Sep 9, 2009 (gmt 0)

10+ Year Member



Okay guys I managed to get the authentication working using
in test, in live however,
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"

$dbuser="user";
$dbpass="password";
$dbname="pharma"; //the name of the database
$connect = mysql_connect("localhost", $dbuser, $dbpass)
or die("Connection Failure to Database");
mysql_select_db($dbname, $connect) or die ($dbname . " Database not found. " . $dbuser);

$auth = false; // Assume user is not authenticated
$name =$_SERVER['PHP_AUTH_USER'];
$pass = $_SERVER['PHP_AUTH_PW'];
if ($name !="" && $pass!= "") {

// the query

$query = "SELECT name, password FROM Admin WHERE
Name = '$name' AND
Password = '$pass'";

// Execute the query and put results in $result

$result = mysql_query( $query )

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="Pharmacy Admin"' );
header( 'HTTP/1.0 401 Unauthorized' );

} else {

session_register('authorised');
header("location:sucess.php?title=go");


}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Login</title>
<link href="../../../../css/1.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.style1 {font-size: 16}
-->
</style>
</head>

<body>
<div id="header3">
<div align="center">
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><h2><?php echo "Authorization Required"; ?></h2></p>
<p>&nbsp;</p>
<p class="style1">The details you entered are incorrect <a href="../../../../index.php">click here</a> to return to the home page</p>
</div>
</div>
</body>
</html>

when I change the values to reflect the remote server, i get the "failed to execute query" message.

There is nothing wrong with the db, i can use it to make tables slightly different way on other pages that use an include connection.php which work fine in test and live.

Once again many thanks in advance

Jon