Forum Moderators: coopster

Message Too Old, No Replies

Member Login Section

No MySQL error message so problem unknown

         

jalalm

1:15 pm on Dec 27, 2007 (gmt 0)

10+ Year Member



Hi
I have been trying to get this script to work but I have problems. I can get it to either show "The username and password entered do not match those on file" or to show "Please try again". THere are MySQL error messages.

<?php # Script 7.7 - login.php
if (isset($_POST['submit'])) {
// This file contains the database access information for the database. This file also establishes a connection to MySQL and selects the database.

// Set the database access information as constants.
define ('DB_USER', '********');
define ('DB_PASSWORD', '*******');
define ('DB_HOST', 'localhost:/tmp/mysql5.sock');
define ('DB_NAME', '*********');

// Make the connnection and then select the database.
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );
mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );

function escape_data ($data) {
global $dbc;
if (ini_get('magic_quotes_gpc')) {
$data = stripslashes($data);
}
return mysql_real_escape_string($data, $dbc);
}
$message = NULL;
if (empty($_POST['username'])) {
$username = FALSE;
$message .= '<p>You forgot to enter your username!</p>';
} else {
$username = escape_data($_POST['username']);
}
if (empty($_POST['password'])) {
$password = FALSE;
$message .= '<p>You forgot to enter your password!</p>';
} else {
$password = escape_data($_POST['password']);
}
if ($username && $password) { // If everything's OK.
$query = "SELECT mem_id, first_name FROM members WHERE username='$username' AND password = PASSWORD('$password')";
$result = @mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
if ($row) {
// Start the session, register the values & redirect.
session_start();
$_SESSION['first_name'] = $row[1];
$_SESSION['mem_id'] = $row[0];
header ("Location: ["...] . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "loggedin.php");
exit();
} else {
$message = '<p>The username and password entered do not match those on file.</p>';
}
mysql_close();
} else {
$message .= '<p>Please try again.</p>';
}
}
$page_title = 'Login';
include ('header.inc');The username and password entered do not match those on file
if (isset($message)) {
echo '<font color="red">', $message, '</font>';
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<fieldset><legend>Enter your information in the form below:</legend>
<p><b>User Name:</b> <input type="text" name="username" size="10" maxlength="20" value="<?php if (isset($_POST['username'])) echo $_POST['username'];?>" /></p>
<p><b>Password:</b> <input type="password" name="password" size="20" maxlength="20" /></p>
<div align="center"><input type="submit" name="submit" value="Login" /></div>
</fieldset></form><!-- End of Form -->
<?php
include ('footer.inc');
?>

Any help would be much appreciated

jalalm

1:23 pm on Dec 27, 2007 (gmt 0)

10+ Year Member



Hi Back again.
I changed the message wording and found the "The username and password entered do not match those on file." was actually coming from the "include ('header.inc');The username or password entered dont match those on file" statement toward the end before the <form> section.
Thanks
James Smith

PHP_Chimp

1:36 pm on Dec 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As I started writing this a while ago and had to go fix something that was broken iv removed most of my message. However the bit below may well interest you (seeing as what I was fixing hasnt been noticed by my customer and its already sorted)

Also seeing as this code is still in testing why are you using @ to suppress errors?

$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );

The @ error suppression is pointless. As you are already coping with errors with the OR die, as if it wont connect then you will die with your chosen error message.
You are using @ in a number of places. So while testing remove all of them then see if your script is broken.

No error reporting = you have no idea what the problem is.

For live environments I try at almost all cost to avoid @. Get the errors written to a system log, emailed to you, whatever, just dont hide them. As you will only hear about it from your customers when it is dead, so if your script report back to you when it starts to break then when you customers ring you complaining you have already fixed the problem...they think that you are psychic, you know you just have good error reporting ;)

jalalm

2:28 pm on Dec 27, 2007 (gmt 0)

10+ Year Member



Thank you for getting back to me
I removed the "@" Error message "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/islamaustralia/www/www/test/login2.php on line 38" Which is halfway down at "if ($row) {"

I am sorry but these messages do not mean much to me. There must be a mistake before this line but I do not know where.
Regards
James Smith

jalalm

2:47 pm on Dec 27, 2007 (gmt 0)

10+ Year Member



The original script had "if ($u && $p)" a couple of lines up but I could not figure out where the $u and $p came from so I replace it with $username and $password which I use throughout the rest of the script.
If I go back to using $u && $p the scrip goes to "please try again" and no MySQL errors.
I do not know if that is helpful. I think it does not recognise $u or $p so skips the "$query" section and goes straight to the "else" part of the "if" statement.

d40sithui

3:57 pm on Dec 27, 2007 (gmt 0)

10+ Year Member



1) first thing i notice is this line.

$row = mysql_fetch_array ($result, MYSQL_NUM);

what is MYSQL_NUM? I dont see it defined anywhere, and even if it was, what should go here is the resource link ($dbc). This might be why you're getting the invalid resource error. you can get away with leaving that blank if you dont plan to make any additional db connections.

2) in regards to the $u/$p vs the $username/$password naming convention, i think this is what happened. you will only get the error "Please try again" if either one of those are not set. so possibly, that means that when you're retrieving the posted data, you may have not received $_POST['username'] but rather did $_POST['u'], which is not the name of the username field on the form. the same could be for the password field.

jalalm

4:13 pm on Dec 27, 2007 (gmt 0)

10+ Year Member



Hi
I have seen that (MYSQL_NUM) used quite a bit in scripting in tutorials but it did not mean anything to me either so I did not know how to alter anything there.
Do you mean the statement should be:
1 $row = mysql_fetch_array ($result, $dbc);
2 $row = mysql_fetch_array ($dbc);
3 $row = mysql_fetch_array ();

I will try from 1 to 3 and see what happens.

James Smith

jalalm

4:27 pm on Dec 27, 2007 (gmt 0)

10+ Year Member



hi
I just used" $result = mysql_query ($query); " and it logged in. Will that carry the session variable accross member pages so that they do not need to continually login?
Thanks for your help. Much appreciated
James Smith

PHP_Chimp

4:30 pm on Dec 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The MYSQL_NUM is a php predefined constant. So if you only want the numeric index's returned and not the named index's returned then that is what you want to use. The default behavior is to return both, so this uses more resources, although it does allow you to then access both from php.

The mysql_fetch_array needs to resource result (the id that you get from the mysql_query) as the first argument, then you can specify your MYSQL_NUM if you want the numeric indexes.
Have a look at the manual [uk.php.net]page, as there are a lot of examples on there.

The SESSION variables will be available across multiple page requests, so long as you put session_start() at the top of each page so that sessions are active. So you can close the mysql connection after getting your results if you want to.

[edited by: PHP_Chimp at 4:32 pm (utc) on Dec. 27, 2007]

d40sithui

6:26 pm on Dec 27, 2007 (gmt 0)

10+ Year Member



ha so it does do something. guess you learn something new everyday! glad it's finaly working for you jalalm