Solved

password generation

Posted on 2007-11-16
9
214 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AJAX Wordpress Not Reading Variable 2 59
restriction of entering a a page 5 34
How do i use the await event in php echo 5 19
Get data from two MySQL tables 6 30
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

696 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