If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Why won't this script work?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-05-04, 22:14
hookedupphat hookedupphat is offline
Registered User
 
Join Date: Sep 2004
Posts: 1
Why won't this script work?

It's for a simple register/login script I'm working on. All the information (username, email, and password) is going into the database fine. The password is going in md5($_POST['userpass']); so it's encrypted. Here's the problem I have:


login.html:
********
<form action="login.php" method="post">
Username: <input type="text" name="username" size="10">
Password: <input type="password" name="userpass" size="10">
<input type="submit" value="submit" name="submit">
</form>
********

login.php:
********
<?
//replace username and password with your mysql name and password
$conn = mysql_connect("localhost","***","***");

//select the database
$db = mysql_select_db("***");

$username = $_POST['username'];

$email = $_POST['email'];

$userpass = md5($_POST['userpass']);

$sql = "select * from users where username='$username' and password='$userpass'";

$result = mysql_query($sql);

if (mysql_num_rows($result)!= 1) {

$error = "Login failed";

echo "Login failed";

} else {

$_SESSION['username'] = "$username";

$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];

// any other data needed to navigate the site or

// to authenticate the user can be added here

echo "Welcome";

}?>
********

When I run the script (yes the information is in the database), it says Login failed, which means mysql_num_rows($result)!= 1), however...it does. I've taken out the password part in the $sql line and it seems to work fine, so something with the md5 encryption isn't working. Is there a better/another way to do this, or is there something really wrong here?

Thanks!
Reply With Quote
  #2 (permalink)  
Old 09-06-04, 00:44
sundialsvcs sundialsvcs is offline
Registered User
 
Join Date: Oct 2003
Posts: 706
Print things. Print the actual query string and the actual num-rows value returned. Run the query separately to be sure that the result is exactly as you expect. Never Assume.
__________________
ChimneySweep(R): fast, automatic
table repair at a click of the
mouse! http://www.sundialservices.com
Reply With Quote
  #3 (permalink)  
Old 09-06-04, 11:42
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
What version of MySQL are you using? Check the manual about long passwords. I seem to remember that there is a problem with a particular version may be 4.x may be 5.x that you need to use OLD PASSWORD when creating a password or your script in PHP can't recognize the password. This is because of some encryption of the password in MySQL.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On