Jump to content

Storing a variable from a function


billsinc

Recommended Posts

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

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());
		}
}

$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')";

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 );

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.