Link to home
Start Free TrialLog in
Avatar of eaweb
eawebFlag for undefined

asked on

password generation

Hi,

I have the following pw generation code:

<?PHP
//set the random id length
$random_id_length = 10;

//generate a random id encrypt it and store it in $rnd_id
$rnd_id = crypt(uniqid(rand(),1));

//to remove any slashes that might have come
$rnd_id = strip_tags(stripslashes($rnd_id));

//Removing any . or / and reversing the string
$rnd_id = str_replace(".","",$rnd_id);
$rnd_id = strrev(str_replace("/","",$rnd_id));

//finally I take the first 10 characters from the $rnd_id
$rnd_id = substr($rnd_id,0,$random_id_length);

echo "Random Id: $rnd_id";
?>

Now I have two problems:

When generating a new pw I need to check first with my database if the pw wasn’t already generated.

I query against the database with this statement:

Select count (*) from pw where uid = $aaa and genpw= $rnd_id

Here if the result is 0 then the generated pw is ok but if the result is not 0 i must generate another.

Now if the pw was already generated I need another pw to be generated and check it again against the database. I need to do this until the pw isn’t already generated.

My second problem is:

When resetting pw I must have the option to select multiple users via check boxes and reset them. What I don’t know is how to generate different pw for all the selected users at the same time and update them with their new pw into the database.

Is what I want to do possible? If yes how can I do it?
Avatar of steelseth12
steelseth12
Flag of Cyprus image

this should solve your first problem
<?PHP
 
function generate_pw($uid) {
 
//set the random id length
	$random_id_length = 10;
	
	//generate a random id encrypt it and store it in $rnd_id
	$rnd_id = crypt(uniqid(rand(),1));
	
	//to remove any slashes that might have come
	$rnd_id = strip_tags(stripslashes($rnd_id));
	
	//Removing any . or / and reversing the string
	$rnd_id = str_replace(".","",$rnd_id);
	$rnd_id = strrev(str_replace("/","",$rnd_id));
	
	//finally I take the first 10 characters from the $rnd_id
	$rnd_id = substr($rnd_id,0,$random_id_length);
 
	$query = mysql_query("SELECT * FROM pw WHERE uid = ".$uid ." AND genpw = ".$rnd_id);
	
	if(mysql_num_rows($query)) {
	
		generate_pw($uid)
	
	}else{
	
		return $rnd_id;
	
	}
 
 
}
 
 $rnd_id  = generate_pw($aaa);
 
echo "Random Id: $rnd_id";
?>

Open in new window

Your second problem i didnt understand ... can you explain a bit more and perhaps show us your code
Avatar of eaweb

ASKER

for example:
i have tree user in my db. now i need to reset two of the users' password.
in my form i check the checkbox of the two users i need to reset their password.
What I dont know is how to generate different pw for the selected users at the same time using the pw gen code i have and update them in the db with their new pw.

what does below code do? it check if the query has results? don't have to compare like:
$num = mysql_num_rows($query);
if($num == 0)
or somthing like this
Zero (0) , empty , null all evaluate to false in php anything else evaluates to true

so

if(mysql_num_rows($query)) {

if it returns > 0 then it will evaluate to true else if it is 0 it will evaluate to false.

>>in my form i check the checkbox of the two users i need to reset their password.
whats the name of each checkbox and what is the value ?

if the cheboxes look like this

<input type="checkbox" name="user[]" value="1" />
<input type="checkbox" name="user[]" value="2" />

etc

then you can do

$users = $_POST["user"];

foreach($users as $value) {


      $new_pass = generate_pw($value);
      
      mysql_query("UPDATE pw SET genpw ='".$new_pass."' WHERE uid='".$value."'");

      
}

Avatar of eaweb

ASKER

steelseth12,

i also try to check all or uncheck all with the following code using the check format you gave me for if i want to generate different password for, but it is giving me error at the first "for (i = 0; i < field.length; i++)" and it does not check or uncheck all. can you help me out?

<script language="JavaScript" type="text/javascript">

var checkflag = "false";
function check(field) {
  if (checkflag == "false") {
    for (i = 0; i < field.length; i++) {
      field[i].checked = true;
    }
    checkflag = "true";
    return "Uncheck All";
  } else {
    for (i = 0; i < field.length; i++) {
      field[i].checked = false;
    }
    checkflag = "false";
    return "Check All";
  }
}
</script>
<form name=myform action="" method=post>
<table>
  <tr><td>
    <strong>Make a selection</strong><br>
    <input type=checkbox name="list[]" value="1">Java<br>
    <input type=checkbox name="list[]" value="2">JavaScript<br>
    <input type=checkbox name="list[]" value="3">ASP<br>
    <input type=checkbox name="list[]" value="4">HTML<br>
    <input type=checkbox name="list[]" value="5">SQL<br>
    <br>                                                    
    <input type=button value="Check All" onClick="this.value=check(this.form.list)">
  </td></tr>
</table>
</form>
Avatar of eaweb

ASKER

or can your code be used like this:

<input type="checkbox" name="user0" value="1" />
<input type="checkbox" name="user1" value="2" />
<input type="checkbox" name="user2" value="1" />
<input type="checkbox" name="user3" value="2" />

in the password generation part?????????
Avatar of eaweb

ASKER

no,

<input type="checkbox" name="user0" value="1" />
<input type="checkbox" name="user1" value="2" />
<input type="checkbox" name="user2" value="1" />
<input type="checkbox" name="user3" value="2" />

isn't working either.

i think the problem is here

onClick="this.value=check(this.form.list)" at list how can i let it check the list[] value
ASKER CERTIFIED SOLUTION
Avatar of steelseth12
steelseth12
Flag of Cyprus image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of eaweb

ASKER

Excellent, thanks