Jump to content

New password to email ?


coop

Recommended Posts

Hi all,

I'm working on a User authentication system using php and mysql with a flash front end. Everything works fine apart from generating the new password and sending it to email of the person requesting the new password.
This is part of the php that generates the new password and updates MySQL.

[code=php:0]
//New password
function new_password($email){
  GLOBAL $db,$table;
  $email = trim($email);
  $query1 = mysql_query("SELECT userName from $table WHERE userMail = '$email'");
  if(mysql_num_rows($query1)<1){
      return "error=email not present into database";
  }
  $row = mysql_fetch_array($query1);
  $username=$row["userName"];
  $query = mysql_query("SELECT * FROM $table WHERE userName = '$username' AND userMail = '$email'");
  $rand_string = '';
  // ---
  // generating a random 8 chars lenght password
  // ---
  for($a=0;$a<7;$a++){
      do{
        $newrand = chr(rand(0,256));
      } while(!eregi("^[a-z0-9]$",$newrand));
      $rand_string .= $newrand;
  }
  $pwd_to_insert = md5($rand_string);
  $new_query = mysql_query("UPDATE $table SET userPassword = '$pwd_to_insert' WHERE userName = '$username' AND userMail = '$email'");
  if(!$new_query){
      return "error=unable to update value";
  }else{
  //return "userName=$username&new_pass=$rand_string"; // this send it back to the flash app.
      // but I wanted to send it by email ?
$to = $email;
$subject = "Password";
$body = $rand_string;
if (mail($to, $subject, $body)) {
  echo("Message successfully sent!");
  } else {
  echo("Message delivery failed...");
  }
  }
}
[/code]


Link to comment
Share on other sites

It doesnt work because you haven't set up the password generation part correctly. Do something like this:
[code]<?php
$salt = "abcdefghijklmnop123456&^%$#";
$rand_string = substr(md5(rand(0,100).time().sha1(str_shuffle($salt))),0,8);
?>[/code]

This will create a 8 chars long, alpha-numeric and very random password.

Orio.
Link to comment
Share on other sites

Orio,

the password generating works, it's just this part of the code where I'm trying to send it by email.

[code=php:0]
  //return "userName=$username&new_pass=$rand_string"; // this send it back to the flash app.
      // but I wanted to send it by email ?
       
$to = $email;
       
$subject = "Password";
       
$body = $rand_string;
       
if (mail($to, $subject, $body)) {
       
  echo("Message successfully sent!");
       
  } else {
       
  echo("Message delivery failed...");
       
  }
  }
}
[/code]

this bit sends it back to the LoadVars object in flash that called the function. When I do it this way the password is generated and shown in the falsh app., so I sure that the password generation works, but I  want to send t by email instead of showing it in flash.

[code=php:0]
return "userName=$username&new_pass=$rand_string";
[/code] 
Link to comment
Share on other sites

Your probably going to at least need to set some [i]From[/i] headers. eg;

[code=php:0]
$to = $email;
$subject = "Password";
$body = $rand_string;
headers= "From: youremail@yoursite.com\r\n";       
if (mail($to, $subject, $body, $headers)) {
[/code]
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.