I want to encrypt the password being sent into the database, with the standard WordPress salt, just like WordPress does it when you create a new user. I know I can find my salt in the wp-config.php. So I don’t need to generate the salt; I just need to encrypt the password.
So that when I create mypassword0, what is sent to the database is the string of text encrypted by my WordPress salt.
Here is my original code that works. (thank you Yadav Chetan for your help!) Now I just need to add the salt encryption code.
<?php
if(isset($_POST['submit'])){
$query = "INSERT INTO mytable_one
(user, pass)
VALUES
('".$_POST['user']."', '".$_POST['pass']."')";
$query = "INSERT INTO mytable_two
(fname, lname)
VALUES
('".$_POST['fname']."', '".$_POST['lname']."')";
mysql_query($query);
}else{
?>
<div class="content">
<form method="post">
<div><strong>First Name:</strong><span class="errortext">*</span></div>
<div><input id="first-name" name="fname" type="text" /></div>
<div><strong>Last Name:</strong><span class="errortext">*</span></div>
<div><input id="last-name" name="lname" type="text" /></div>
<div><strong>User:</strong><span class="errortext">*</span></div>
<div><input id="user-login" name="user" type="text" /></div>
<div><strong>Password:</strong><span class="errortext">*</span></div>
<div><input id="user-pass" name="pass" type="text" /></div>
<div><input id="submit-button" value="submit" type="submit" />
</div>
</form>
<?php }?>
UPDATED:
RRikesh suggested I change the mysql_* to WPDB code. So I tried to change it over to wpdb, and also I need to integrate it in with the other code. So can you help me fix this updated code?
<?php
if(isset($_POST['submit'])){
$firstname = $_POST['fname'];
$lastname = $_POST['lname'];
$username = $_POST['user'];
$password = $_POST['pass'];
$wpdb->query(
$wpdb->prepare(
"INSERT INTO mytable_one
(user, pass) VALUES (%s, %s)",
$username,
wp_hash_password($password)
)
);
$wpdb->query(
$wpdb->prepare(
"INSERT INTO mytable_two
(fname, lname) VALUES (%s, %s)",
$firstname,
$lastname,
)
);
}else{
?>
<div class="content">
<form method="post">
<div><strong>First Name:</strong><span class="errortext">*</span></div>
<div><input id="first-name" name="fname" type="text" /></div>
<div><strong>Last Name:</strong><span class="errortext">*</span></div>
<div><input id="last-name" name="lname" type="text" /></div>
<div><strong>Username:</strong><span class="errortext">*</span></div>
<div><input id="user-login" name="user" type="text" /></div>
<div>Password:</div>
<div><input id="user-pass" name="pass" type="text" /></div>
<div><input id="submit-button" value="submit" name="submit" type="submit" /></div>
</form>
<?php }?>
UPDATE2
I was unable to get the WPDB method to work. however, using my otd method I was able to has the password. Here is the working code:
<?php
if(isset($_POST['submit'])){
$password = $_POST['user_pass'];
$hash = wp_hash_password('$password');
$query = "INSERT INTO wp_users
(fname, lname, user, pass) VALUES ('".$_POST['fname']."', '".$_POST['lname']."', '".$_POST['user']."', '".$hash."')";
mysql_query($query);
}else{
?>
Perhaps I should open a new question about the WPDB because this question was about hashing the password, which is solved.
Use
wp_hash_password()
to hash your password.Don’t use
mysql_*
functions as they were deprecated in PHP 5.5.0, and were removed in PHP 7.0.0.Use the WPDB Class instead.
you should use bcrypt for securing passwords
here is an example class that use for my projects.