billsinc Posted December 3, 2010 Share Posted December 3, 2010 This seems really straight forward but I can't figure out why it's happening. As a part of an account registration script, I'm randomly generating a password using a function called, generatePassword($length,$strength); I hash the password (md5) before putting it in the database but I also email the non-hashed version to the user. My problem is that each time I call the variable, it generates a new password so the one in the database does not match the one received by the user, $user_password_plain = generatePassword($length,$strength); $user_password = md5($user_password_plain); Is there a different way to store the variable so that I can access it later in the script and it will be the same? I tried using a session variable but that didn't seem to work either. Thanks! Link to comment https://forums.phpfreaks.com/topic/220602-storing-a-variable-from-a-function/ Share on other sites More sharing options...
requinix Posted December 3, 2010 Share Posted December 3, 2010 A variable will only change its value if you change its value. It'll help to see more code. Link to comment https://forums.phpfreaks.com/topic/220602-storing-a-variable-from-a-function/#findComment-1142716 Share on other sites More sharing options...
billsinc Posted December 3, 2010 Author Share Posted December 3, 2010 I thought that was the case but I'm getting a different value in the database from the one I'm getting via email. This version of the code does not store the hashed version, which I was using to diagnose the problem. More complete code: // Password complexity $length = 8; $strength = 2; static $user_password_plain = generatePassword($length,$strength); // Create MD5 hash password for database $user_password = md5($user_password_plain); //if (!get_magic_quotes_gpc()) { //$user_password = addslashes($user_password_plain); //} // If new account, enter information into database if ($notification_type == "new_account_notification") { $insert = "INSERT INTO users (accountcode,firstname, lastname, email, username, password, companyname, approved) VALUES ('".$account_code."','".$first_name."','".$last_name."','".$email."','".$email."', '".$user_password_plain."','".$company_name."','0')"; $add_member = mysql_query($insert); } // If subscription, sleep for X seconds to wait for account creation then approve account if ($notification_type == "new_subscription_notification" || $notification_type == "reactivated_account_notification") { //sleep(15); // Not working $check = mysql_query("SELECT accountcode FROM users WHERE accountcode = '$account_code'") or die(mysql_error()); $check2 = mysql_num_rows($check); // If the user exists, set approved=1 if ($check2 != 0) { $update_approved = mysql_query("UPDATE users SET approved=1 WHERE accountcode = '$account_code'") or die(mysql_error()); } // If the user does not exist, die //if ($check2 = 0) { //die('User record not found!'); //} // And send plain text email with login instructions $to = $email; $subject = "Your Login Information"; //Each line should be separated with \n $message = "Hello ".$first_name.",\n\nWelcome to XYZ.\n\nYou may login any time at http://www.XYZ.com using the following information:\n\nUsername: ".$email."\nPassword: ".$user_password_plain."\n\nIf you need technical support, please send an email to [email protected]."; $headers = "From:"; $mail_sent = @mail( $to, $subject, $message, $headers ); } // If payment received, do nothing (for now) if ($notification_type == "successful_payment_notification") { } // Remove approval for canceled and expired subscriptions if ($notification_type == "canceled_subscription_notification" || $notification_type == "expired_subscription_notification") { $check = mysql_query("SELECT accountcode FROM users WHERE accountcode = '$account_code'") or die(mysql_error()); $check2 = mysql_num_rows($check); // If the user exists, set approved=0 if ($check2 != 0) { $update_approved = mysql_query("UPDATE users SET approved=0 WHERE accountcode = '$account_code'") or die(mysql_error()); } } Link to comment https://forums.phpfreaks.com/topic/220602-storing-a-variable-from-a-function/#findComment-1142733 Share on other sites More sharing options...
BlueSkyIS Posted December 3, 2010 Share Posted December 3, 2010 $insert = "INSERT INTO users (accountcode,firstname, lastname, email, username, password, companyname, approved) VALUES ('".$account_code."','".$first_name."','".$last_name."','".$email."','".$email."', '".$user_password_plain."','".$company_name."','0')"; should probably be $insert = "INSERT INTO users (accountcode,firstname, lastname, email, username, password, companyname, approved) VALUES ('".$account_code."','".$first_name."','".$last_name."','".$email."','".$email."', '".$user_password."','".$company_name."','0')"; Link to comment https://forums.phpfreaks.com/topic/220602-storing-a-variable-from-a-function/#findComment-1142740 Share on other sites More sharing options...
billsinc Posted December 3, 2010 Author Share Posted December 3, 2010 That's the way the "real" code is. I'm just using the this version so I can compare the value sent in the email directly to the value in the database. Link to comment https://forums.phpfreaks.com/topic/220602-storing-a-variable-from-a-function/#findComment-1142743 Share on other sites More sharing options...
billsinc Posted December 3, 2010 Author Share Posted December 3, 2010 Okay, I fixed it by sending the email in the first IF function...AKA moving it up so it's executed at the same time. Link to comment https://forums.phpfreaks.com/topic/220602-storing-a-variable-from-a-function/#findComment-1142745 Share on other sites More sharing options...
requinix Posted December 3, 2010 Share Posted December 3, 2010 That static keyword may be messing you up. Any particular reason you need it? Link to comment https://forums.phpfreaks.com/topic/220602-storing-a-variable-from-a-function/#findComment-1142753 Share on other sites More sharing options...
billsinc Posted December 3, 2010 Author Share Posted December 3, 2010 I already fixed it. I was trying to use a static variable but realized that wouldn't work either because the variable was not part of a class or function. Link to comment https://forums.phpfreaks.com/topic/220602-storing-a-variable-from-a-function/#findComment-1142761 Share on other sites More sharing options...
mrherman Posted December 4, 2010 Share Posted December 4, 2010 billsinc -- Just interested in this thread...Could you post the final piece of code? Thanks! Link to comment https://forums.phpfreaks.com/topic/220602-storing-a-variable-from-a-function/#findComment-1142812 Share on other sites More sharing options...
billsinc Posted December 8, 2010 Author Share Posted December 8, 2010 Just move this section up under the account creation section: // And send plain text email with login instructions $to = $email; $subject = "Your Login Information"; //Each line should be separated with \n $message = "Hello ".$first_name.",\n\nWelcome to XYZ.\n\nYou may login any time at http://www.XYZ.com using the following information:\n\nUsername: ".$email."\nPassword: ".$user_password_plain."\n\nIf you need technical support, please send an email to [email protected]."; $headers = "From:"; $mail_sent = @mail( $to, $subject, $message, $headers ); Link to comment https://forums.phpfreaks.com/topic/220602-storing-a-variable-from-a-function/#findComment-1144272 Share on other sites More sharing options...
mrherman Posted December 8, 2010 Share Posted December 8, 2010 Hey, thanks for replying! Link to comment https://forums.phpfreaks.com/topic/220602-storing-a-variable-from-a-function/#findComment-1144285 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.