Sponsored by NuSphere - PHP Software for PHP Application Developers - On Sale This Week for $100



Go Back   PHP-Editors > Programming Help > PHP Programming Help

PHP Programming Help Post any question relating to PHP Programming here and hopefully someone can help.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 2008-01-29, 11:21 PM
Junior Member
 
Join Date: Jan 2008
Posts: 2
jrp91384 is on a distinguished road
Question Authenticating a User Against a MySQL Table

Hi, I’m trying to authenticate a user against a MySQL database and get the following errors. Note, I’m a beginner in the early stages and this is script from a book I’m reading. Any help would be greatly appreciated!
Thank you…

My Errors:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\xampp\htdocs\test\passwordtest2.php on line 27

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\test\passwordtest2.php on line 30

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test\passwordtest2.php:27) in C:\xampp\htdocs\test\passwordtest2.php on line 6

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test\passwordtest2.php:27) in C:\xampp\htdocs\test\passwordtest2.php on line 7


My Database:
authenticationtest

My authentication table settings are as follows:
rowID TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
commonname VARCHAR(35) NOT NULL,
username VARCHAR(9) NOT NULL,
pswd VARCHAR(32) NOT NULL,
PRIMARY KEY(rowID));

My php code is as follows (with the exception of my username and password):
<?php
/* Because the authentication prompt needs to be invoked twice,
embed it within a function.
*/
function authenticate_user() {
header('WWW-Authenticate: Basic realm="Secret Stash"');
header("HTTP/1.0 401 Unauthorized");
exit;
}

/* If $_SERVER['PHP_AUTH_USER'] is blank, the user has not yet been prompted for
the authentication information.
*/
if (! isset($_SERVER['PHP_AUTH_USER'])) {
authenticate_user();
} else {
// Connect to the MySQL database by entering the MySQL server, username, and password. Also enter the db on next line.
$conn = mysql_connect("localhost", "myusername", "mypassword");

$db = mysql_select_db("authenticationtest");

// Create and execute the selection query.
$query = "SELECT username, pswd FROM userauth
WHERE username='$_SERVER[PHP_AUTH_USER]' AND
pswd=md5('$_SERVER[PHP_AUTH_PW]')";

$result = mysql_query($conn, $query);

// If nothing was found, reprompt the user for the login information.
if (mysql_num_rows($result) == 0) {
authenticate_user();
}
else {
echo "Welcome to the Authentication Test!";
}
}
?>
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 2008-01-31, 01:23 AM
Xnuiem's Avatar
Senior Member
 
Join Date: May 2004
Location: DFW, Texas
Posts: 1,104
Xnuiem will become famous soon enough
Send a message via Yahoo to Xnuiem
Default

$result = mysql_query($conn, $query);

should be

$result = mysql_query($query, $conn);

PHP: mysql_query - Manual
__________________
I rarely give code examples.
No, I have never used IIS or Windows of any kind as a web server. Get a real OS!
Please don't PM me, I won't respond.
Reply With Quote
  #3 (permalink)  
Old 2008-01-31, 01:40 AM
Junior Member
 
Join Date: Jan 2008
Posts: 2
jrp91384 is on a distinguished road
Default

Quote:
Originally Posted by Xnuiem View Post
$result = mysql_query($conn, $query);

should be

$result = mysql_query($query, $conn);

PHP: mysql_query - Manual

I switched it to: $result = mysql_query($query, $conn);
but when logging in it will not accept the username and password and goes directly to a blank page on the third attempt.
Any suggestions?
Reply With Quote
  #4 (permalink)  
Old 2008-02-18, 09:31 AM
Junior Member
 
Join Date: Feb 2008
Posts: 5
prafulkr is on a distinguished road
Default

use
ob_start()

for output buffering. So warning 3 and 4 will be omitted .
and for 1 and 2 because your query does not fetch any row so this warning appears on screen.

you just print your query on browser and run that query in data base. You got your result.
__________________
Regards.
Praful

When I was born, I was cryed and everyone around me was smiling.
Live our life so at the end, I am the one who is smiling and everyone around me is crying.
Reply With Quote
Must read Review for Serious PHP Developers


NuSphere PhpED 5.0 : The Staff of php-editors.com recently spent a few days working with NuSphere PhpED 5.0 (a popular PHP IDE) and NuCoder 1.4 (a PHP Encoding Utility), read up on all the details.

Sponsored Links
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 07:38 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0
© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.