Jump to content

[SOLVED] new if() thread, making myself NUTS


Lodius2000

Recommended Posts

This always redirects to changepw.php

 

<?php
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$hash = hash("sha512",$password.$salt);

$q = mysql_query("SELECT `password` FROM `users` WHERE `username` = $username");
if ( $a[0] == $hash ){
$_SESSION['username'] = $username;

print "you have successfully logged-in ";
print '<a href="changepw.php">Change Password</a>';

} else {
	mysql_query("UPDATE `users` SET `temp_usage` = '1' WHERE `username` = $username and `temp_password` = $hash");
	$_SESSION['username'] = $username;
	header('Location: changepw.php');
}

?>

 

and the query that updates temp_usage, does not set it to 1

whats wrong

Link to comment
Share on other sites

sorry cv supposed to be $q[0].... for some stupid reason I changed the variable for this forum, they do match up in my script though

 

//repost of the code

<?php
   $username = trim($_POST['username']);
   $password = trim($_POST['password']);
   $hash = hash("sha512",$password.$salt);

   $q = mysql_query("SELECT `password` FROM `users` WHERE `username` = $username");
   if ( $q[0] == $hash ){
   $_SESSION['username'] = $username;
   
   print "you have successfully logged-in ";
   print '<a href="changepw.php">Change Password</a>';
   
   } else {
      mysql_query("UPDATE `users` SET `temp_usage` = '1' WHERE `username` = $username and `temp_password` = $hash");
      $_SESSION['username'] = $username;
      header('Location: changepw.php');
   }

?>

Link to comment
Share on other sites

Try using a mysql function to extract that specific row from the result resource returned by mysql_query;

 

<?php

   $q = mysql_query("SELECT `password` FROM `users` WHERE `username` = $username");
   $result = mysql_result($q,0,'password');
   if ( $result == $hash ){

?>

Link to comment
Share on other sites

this is the bane of using a database extraction... everytime i have a sql question on these forums, if I mention i use peardb i get no responses, because my sql looks weird. if I dont mention it and I try to make it look like i use the php mysql functions i invariably screw up and people correct my function usage. I know how to use my dba, I dont know how to use php's mysql functions but i try to fudge it.

 

I think there is a problem with how my query result is interacting with my if() statement (possibly a sql problem all together

 

so in my real script $q is fetched into an associative array, so the code looks as such

if ($q['password'] == $hash)

 

now to me that says, 'if the password entered in the form matches the password in the database, display a successful login', now to the else clause 'else the password entered in the form does not match the database then do the second query and redirect to the password changing script'

 

right, or am i screwed up somewhere

 

EDIT: CV it seems like you have changed your sig like 9 times tonight, all of them worthy of being up there for a quite a while, i was still laughing at the last one, now you have the stick pron joke

Link to comment
Share on other sites

alright then that sounded like a passive request so here it is down and dirty

 

<?php
function process_form(){
global $db;
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$hash = hash("sha512",$password.$salt);

$a = $db->getOne("SELECT password FROM users WHERE username = ?", array($username));
if ($a['password'] == $hash ){
$_SESSION['username'] = $username;

print "you have successfully logged-in ";
print '<a href="changepw.php">Change Password</a>';	
} else {
	$db->query("UPDATE users SET temp_usage = 1 WHERE username = ? and temp_password = ?", array($username, $hash));
	$_SESSION['username'] = $username;
	header('Location: changepw.php');
}

}
?>

Link to comment
Share on other sites

well thats just interesting

 

vardump hash prints out the correct hash

$a['password'] on the other hand prints out '6', which is the first character of the hash, but not the first character of id, the only numeric field in the table that doesnt contain 0 or 1

Link to comment
Share on other sites

hash and $a are not the same, though they should be, I made a new account and tried this whole thing again

 

still different

 

if register.php hashes the pw before putting it in the db like this: (direct copy paste)

 

$password    = $_POST['password'];

$hash = hash("sha512",$password.$salt);

 

and $hash is created in the login script like this: (direct copy paste)

 

$password = $_POST['password'];

$hash = hash("sha512",$password.$salt);

 

how am i getting different hash values

 

Link to comment
Share on other sites

Make sure your not hashing them twice, that the are the same case, length and use some sort of PearAdmin interface to make sure the hash stored in the db is the same, also make sure the db field isn't set to 'password' or anything, it could hash your hash - if u know what i mean.

 

try echoing the insert query from registration. then echo the results from a select query etc, generally debug this situation and post your findings :).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.