Jump to content
Larry Ullman's Book Forums

Create Passwords Directly In The Db?


Recommended Posts

I have a need to generate many (50+) accounts.  I can do this rather easily directly in the DB using PHPMyAdmin or the like.

 

EXCEPT for the passwords.  

 

I am following the password creation methods using a hash as you have laid out in your book.  Thus I cannot figure a way to create them directly in the DB because of the hash encoding used in the PHP code.

 

Is there a way to create passwords without having to create them in the registration / edit user pages?  Can it be done directly in something like PHPMyAdmin?  

 

Alternatively, is there a way to quickly create multiple accounts?  (They could even have all the same password in this particular case I need).

 

Hopefully the topic and questions make sense.  Thanks!

 

--David 

Link to comment
Share on other sites

Of course. It all depends on your needs, really. Do these accounts need to have specific details or can they have random data? Do you have the password in plain text? You don't give us to much to work with here. ;)

 

Non-the-less, creating the users should happen inside a loop. In that loop, you'd build all the data you'll need to insert for each user. This can happen trough building a custom query or using PDO.

$dsn = 'mysql:dbname=YOUR_DATABASE;host=localhost';
$user = 'dbuser';
$password = 'dbpass';
try {
   $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
   echo 'Connection failed: ' . $e->getMessage();
}

// Define the prepared statement "rules"/schema
// Variables below binds into question mark 1, 2 and 3
$stmt = $dbhandle->prepare("INSERT INTO users (firstname, lastname, password) VALUES (?, ?, ?)");
$stmt->bindParam(1, $firstname);
$stmt->bindParam(2, $lastname);
$stmt->bindParam(3, $password);

// Some kind of user structure. File/array/etc
$users = array(
   0 => array("firstname => "'Jon', "lastname => 'Doe', "password" => 'P4ssw0rd'),
   1 => array("firstname => "'Jon', "lastname => 'Doe', "password" => 'P4ssw0rd'),
   3 => array("firstname => "'Jon', "lastname => 'Doe', "password" => 'P4ssw0rd'),
);

// Intract with your user structure. You'll have to change this a bit
foreach ( $users as $user ) 
{
   // Notice how we bind data to the same variables here as in the schema?
   $firstname = $user['firstname'];
   $lastname = $user['lastname'];
   $password = password_hash($user['password'], PASSWORD_DEFAULT);
   $stmt->execute();
}

// Don't care how the users look like? Along these lines. (I.e skip the user array/file)
for ($i=0; $i<=50; $i++; )
{
   $firstname = 'Firstname';
   $lastname = 'Lastname';
   $password = password_hash('123test, PASSWORD_DEFAULT);
   $stmt->execute();
}

}

Hope that gives you an idea. This is not a working script, but something to lead you along the way.

 

Get back to us if something is unclear.

  • Upvote 1
Link to comment
Share on other sites

Thanks for your response, Antonio.  Your advice is always helpful.  

Sorry I didn't reply sooner; I was sorting out the post-Yosemite upgrade problems.

Anyway, I am not advanced enough to follow your suggestions, but they did get me started enough to come up with my own solution.  I share it here in case it's helpful to others.  

FYI, this was for a group of test users I created, all with a sequential number at the end of their name.  

 
<?php 
include('../includes/database.php');
 
for ($i=1; $i < 51; $i++) { 
 
$pass = "NewPassword4321";
$user = "NameOfSequentialUser" . $i;
$query = "UPDATE users
 SET
 pass='"  .  password_hash($pass, PASSWORD_BCRYPT) .  "'
 WHERE username='$user'
 ";
echo $query;
 
//Write results to DB 
$mysqli->query($query) or die();
$query = "";
reset($mysqli);
echo "Complete for" . $user . "!</br>";
}
 

 

echo "ALL complete";
Link to comment
Share on other sites

 Share

×
×
  • Create New...