Jump to content

Variables changing on their own


micmania1

Recommended Posts

I'm sure its the server/php settings but im going to post my script just incase.

 

What my script does is generates a random code which is then saved in the database and sent in an email.

 

The problem is, that although i don't change the variable '$code', they have different values when saved/sent.

 

Here is my script:

<?php # lostpassword.php 2009/08/16

define("HACK_ATT", true);

require_once('admin/mysql_connect.php');
require_once ('admin/systemvars.php');
require_once ('admin/classes.php');
require_once ('admin/functions.php');


function genCode() {
$chars = 10;
$code = NULL;

$string = '0123456789';

for ($i = 0; $i < $chars; $i++) {
	$rand = mt_rand(0,9);
	$code .= $string[$rand];
}

// Check for duplicates
$query = "SELECT * FROM lostpasswords WHERE code='$code' LIMIT 1";
$result = mysql_query($query);
if ($result) {
	$num = mysql_num_rows($result);
	mysql_free_result($result);
	if ($num == 1) {
		return genCode();
	} else {
		return $code;
	}
} else {
	return FALSE;
}
}

if (isset($_POST['submitted'])) {
if (!is_empty($_POST['email'])) {
	if (valid_email($_POST['email'])) {
		$email = escape_data($_POST['email']);

		$query = "SELECT user_id, username, password, email FROM users WHERE email='$email' LIMIT 1";
		$result = mysql_query($query);
		if ($result) {
			$num = mysql_num_rows($result);
			if ($num == 1) {
				$user_data = mysql_fetch_assoc($result);

				$code = genCode();


				if (!empty(trim($code))) {
					$req_date = strtotime("now");

					$query = "INSERT INTO lostpasswords (code, user_id, req_date) VALUES ('$code', '{$user_data['user_id']}', '$req_date')"; 
					$result = mysql_query($query) or die("Unable to generate password request.");

					require($siteRoot."/includes/class.phpmailer.php");

					$mail = new PHPMailer;

					$mail->From = "[email protected]";
					$mail->FromName = "xxx.com";
					$mail->AddAddress($user_data['email']);
					$mail->AddReplyTo('[email protected]');
					$mail->IsHTML(true);

					$mail->Subject = 'Lost Password';

					$mail->Body = 'This is an automated reply to a lost password request from http://www.xxx.com.<br /><br />

					To reset your password click the following link:<br />
					<a href="http://www.xxx.com/resetpassword.php?req='.$code.'">http://www.xxx.com/resetpassword.php?req='.$code.'</a><br /><br />

					If you did not request your account details, please ignore this email.<br /><br />

					Kind Regards,<br />
					xxx<br />
					http://www.xxx.com';

					if($mail->Send()) {
						tmp::$success[] = 'A link to reset your password has been sent to '.$email.'.';
					} else {
						tmp::$errors[] = 'Unable to send password request.';
					}
				} else {
					tmp::$errors[] = 'Unable to generate password request.';
				}
			} else {
				tmp::$errors[] = 'We could not find your email in our database.';
			}
		} else {
			tmp::$errors[] = 'We could not find your email in our database.';
		}
	} else {
		tmp::$errors[] = 'You have not entered a valid email.';
	}
} else {
	tmp::$errors[] = 'You forgot to enter an email address.';
}
}				


tmp::$content = '<div class="main_title">.:: Lost Password</div>
		<div style="margin: 100px; text-align:center;"><form name="pass" action="lostpassword.php" method="post">
			Your Email: <input type="text" name="email" class="normal" /><br /><br />
			<img src="images/buttons/submit.png" class="button" width="75" height="25" border="0" alt="Password Request" onclick="document.pass.submit();" /><input type="hidden" name="submitted" />
		</form>
		</div>';

tmp::create();
?>

 

A random code is created and sent to the email address no problem, but the code saved to the database is different but always the same meaning I have X number of records in my db table all with the same code.

 

Can anybody shed any light on this?

 

Thank you.

Link to comment
https://forums.phpfreaks.com/topic/170635-variables-changing-on-their-own/
Share on other sites

The problem is, that although i don't change the variable '$code', they have different values when saved/sent.

 

for ($i = 0; $i < $chars; $i++) {
	$rand = mt_rand(0,9);
	$code .= $string[$rand];
}

 

$code always gets an other random number...

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.