?
Solved

password generation

Posted on 2007-11-16
9
Medium Priority
?
218 Views
Last Modified: 2013-12-13
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?
0
Comment
Question by:eaweb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 20

Expert Comment

by:steelseth12
ID: 20299609
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

0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20299615
Your second problem i didnt understand ... can you explain a bit more and perhaps show us your code
0
 

Author Comment

by:eaweb
ID: 20300389
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
0
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 
LVL 20

Expert Comment

by:steelseth12
ID: 20300500
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."'");

      
}

0
 

Author Comment

by:eaweb
ID: 20323119
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>
0
 

Author Comment

by:eaweb
ID: 20323220
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?????????
0
 

Author Comment

by:eaweb
ID: 20327079
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
0
 
LVL 20

Accepted Solution

by:
steelseth12 earned 2000 total points
ID: 20327113
Hi eaweb

For fields like <input type="checkbox" name="user0" value="1" />  try the code below


foreach($_POST as $key=>$value) {
 
	if(preg_match("/^user[0-9]+/",$key)) {
 
		$users[] = $value;	
	
	}
 
}
 
 
 
foreach($users as $value) {
 
 
      $new_pass = generate_pw($value);
      
      mysql_query("UPDATE pw SET genpw ='".$new_pass."' WHERE uid='".$value."'");
 
      
}

Open in new window

0
 

Author Comment

by:eaweb
ID: 20472218
Excellent, thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question