Solved

password generation

Posted on 2007-11-16
9
213 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
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 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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
website maintenance mode 1 25
Read a remote csv file from a https URL 8 49
Custom Wordpress Loop 22 38
How to delete values from an array using php 4 23
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

828 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