PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 308,429 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 3,260 people online right now. Registration is fast and FREE... Join Now!




Forgot Password Script errors

 

Forgot Password Script errors

ninethousandfeet@msn.com

23 Feb, 2009 - 02:14 PM
Post #1

D.I.C Regular
***

Joined: 9 Feb, 2009
Posts: 266



Thanked: 4 times
My Contributions
hello,

i've been working on the same issue for the last few days and i just cannot seem to get it working properly. the code that i am posting is where it stands now, but it has gone back and forth many times with different variations. my goal is to create a forgot password link on my login page that will ask the user to type in their registered email address. if it matches, then email them a random pwd. else, send them an error message. to me, it seems simple enough, but i'm having one heck of a time. i've read many other posts on various forums to try and figure this out on my own, but i either don't get anywhere or i can only get partial results.
as it stands with the code i have below... the error arrays seem to work okay as i'm unable to submit an email without @ or . and i cannot leave the field empty. however, as long as i include the @ and . then the page accepts it, and sends me to the home page as if everything is okay. i have left out the mail processing script on this one because i'm trying to first figure out how to distinguish whether the email submitted by a user is in the database or not and only then should the user be taken to the home page.
any help would be great, as i mentioned i'm pretty much going in circles.
thank you!
CODE

<?php require_once('Connections/connUser.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}    
$colname_getEmail = "-1";
if (isset($_GET['email'])) {
  $colname_getEmail = $_GET['email'];
}
mysql_select_db($database_connUser, $connUser);
$query_getEmail = sprintf("SELECT user_id, username, password, email FROM userTable WHERE email = %s", GetSQLValueString($colname_getEmail, "text"));
$getEmail = mysql_query($query_getEmail, $connUser) or die(mysql_error());
$row_getEmail = mysql_fetch_assoc($getEmail);
$totalRows_getEmail = mysql_num_rows($getEmail);

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form2")) {
// Initialize array for error messages
$error = array();
$_POST['email'] = trim($_POST['email']);
if (empty($_POST['email'])) {
    $error['email'] = 'Please make sure you have filled in an email address.';
}
if (!stristr($_POST['email'],"@") OR !stristr($_POST['email'],".")) {
    $error['emailFormat'] = 'Please make sure your email address is valid';
}

  function makeRandomPassword() {
          $salt = "abchefghjkmnpqrstuvwxyz012345678923456789";
          srand((double)microtime()*1000000);  
          $i = 0;
          while ($i <= 7) {
                $num = rand() % 41;
                $tmp = substr($salt, $num, 1);
                $pass = $pass . $tmp;
                $i++;
          }
          return $pass;
    }
    $random_password = makeRandomPassword();
    $db_password = sha1($random_password);
    
if (!$error) {
  $updateSQL = sprintf("UPDATE userTable SET password='$db_password' WHERE email='$email'",
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['password'], "text"),
                       GetSQLValueString($_POST['user_id'], "int"));

  mysql_select_db($database_connUser, $connUser);
  $Result1 = mysql_query($updateSQL, $connUser) or die(mysql_error());
      
  $updateGoTo = "index.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
  }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Forgot Password and/or Username</title>
</head>

<body>
<style type="text/css">
<!--
.warning {
    color: #F00;
}
-->
</style>
<p>Forgot Password and/or Username</p>
<p><?php
if (isset($error)) {
    echo '<ul>';
    foreach ($error as $alert) {
        echo "<li class='warning'>$alert</li>\n";
    }
    echo '</ul>';
}
?>&nbsp;</p>
<form action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2">
  <table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Email:</td>
      <td><input type="text" name="email" value="<?php echo htmlentities($row_getEmail['email'], ENT_COMPAT, 'UTF-8'); ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td><input type="submit" value="Update record" /></td>
    </tr>
  </table>
  <input type="hidden" name="MM_update" value="form2" />
  <input type="hidden" name="user_id" value="<?php echo $row_getEmail['user_id']; ?>" />
</form>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($getEmail);
?>



User is offlineProfile CardPM
+Quote Post

 
Reply to this topicStart new topic
Replies(1 - 3)

JackOfAllTrades

RE: Forgot Password Script Errors

23 Feb, 2009 - 03:11 PM
Post #2

I exist to Google your problems.
Group Icon

Joined: 23 Aug, 2008
Posts: 5,430



Thanked: 459 times
Dream Kudos: 50
Expert In: Being annoyed with lazy people.

My Contributions
You seem to be mixing $_GET['email'] and $_POST['email'], which could be the issue.

In this code:
php
$colname_getEmail = "-1";
if (isset($_GET['email'])) {
$colname_getEmail = $_GET['email'];
}
mysql_select_db($database_connUser, $connUser);
$query_getEmail = sprintf("SELECT user_id, username, password, email FROM userTable WHERE email = %s", GetSQLValueString($colname_getEmail, "text"));


if the email is coming via a POST, then $colname_getEmail is going to be -1, and that's not going to be right. You could try printing the value of $query_getEmail after the sprintf() call and see what the query is that's being submitted to the server.

User is online!Profile CardPM
+Quote Post

ninethousandfeet@msn.com

RE: Forgot Password Script Errors

23 Feb, 2009 - 04:25 PM
Post #3

D.I.C Regular
***

Joined: 9 Feb, 2009
Posts: 266



Thanked: 4 times
My Contributions
okay, so i did two things; changed the GET to POST to line up with everything else and then i added the print so that i could see what the sprintf() was running and it came back okay. whatever i would type in the field, the query would return SELECT * FROM userTable WHERE email = --whatever i typed in field here-

everything that i have read seems to point in the direction of making my totalRows_getEmail == 0... it seems like the issue has something to do with the script in this area.

i am not sure why i cannot simply show an error message if the email does not match an email in the database. and on the flip side, go ahead with the change password and email script if the email does match the email in the database.

any more ideas? thank you!

CODE

$colname_getEmail = "-1";
if (isset($_POST['email'])) {
  $colname_getEmail = $_POST['email'];
}
mysql_select_db($database_connUser, $connUser);
$query_getEmail = sprintf("SELECT user_id, username, password, email FROM userTable WHERE email = %s", GetSQLValueString($colname_getEmail, "text"));
$getEmail = mysql_query($query_getEmail, $connUser) or die(mysql_error());
$row_getEmail = mysql_fetch_assoc($getEmail);
$totalRows_getEmail = mysql_num_rows($getEmail);

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form2")) {
// Initialize array for error messages
$error = array();
$_POST['email'] = trim($_POST['email']);
if (empty($_POST['email'])) {
    $error['email'] = 'Please make sure you have filled in an email address.';
}
if (!stristr($_POST['email'],"@") OR !stristr($_POST['email'],".")) {
    $error['emailFormat'] = 'Please make sure your email address is valid';
    $status = 'NOTOK';
}
if ($statusOK) {
  function makeRandomPassword() {
          $salt = "abchefghjkmnpqrstuvwxyz012345678923456789";
          srand((double)microtime()*1000000);  
          $i = 0;
          while ($i <= 7) {
                $num = rand() % 41;
                $tmp = substr($salt, $num, 1);
                $pass = $pass . $tmp;
                $i++;
          }
          return $pass;
    }
    $random_password = makeRandomPassword();
    $db_password = sha1($random_password);
    
if (!$error) {
  $updateSQL = sprintf("UPDATE userTable SET password='$db_password' WHERE email='$email'",
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['password'], "text"),
                       GetSQLValueString($_POST['user_id'], "int"));

  mysql_select_db($database_connUser, $connUser);
  $Result1 = mysql_query($updateSQL, $connUser) or die(mysql_error());
      
  $updateGoTo = "index.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
  }
}
}
?>


User is offlineProfile CardPM
+Quote Post

bsaunders

RE: Forgot Password Script Errors

23 Feb, 2009 - 07:21 PM
Post #4

D.I.C Addict
****

Joined: 18 Jan, 2009
Posts: 558



Thanked: 42 times
My Contributions
What happens if you add this:

if ($statusOK && $totalRows_getEmail > 0) {
function makeRandomPassword() {
?
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/24/09 01:53PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month