Solved

password generation

Posted on 2007-11-16
9
210 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
  • 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 500 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now