Localstorage works a lot like cookies. (but they are not the same)
Don't get me wrong localstorage was a phenominal update for modern browsers. Now, developers can easily load massive applications without having to store chunks in files on the server! It is advised not to store secure information such as a users password in the localstorage.
Instead generate a random MD5 hash key set up as an authorizing key for a script.
Have a script in PHP set up to return a password for an account associated with the authorizing key & username. Remember to reset the key after to authorization is made.
Database:
| ID | Username | Password | Email | Key |
| 1 | John | secret | john@gmail.com | 0cc175b9c0f1b6a831c399e269772661 |
For your PHP i would recommend you look into PHP::PDO http://php.net/manual/en/book.pdo.php
PHP: (forgot_password.php)
<?PHP
if(isset($_GET['key']) && isset($_GET['username'])){
$connect = new PDO('mysql:host=localhost;dbname=' . /* DB NAME */,/*DB USERNAME*/, /* DB PASSWORD */);
$user = getall($connect, /* TABLE NAME */,
array(
'PASSWORD'
),
array(
'key'=>$_GET['key'],
'username', $_GET['username']
), 1,
array(
'ASC'=>'ID'
);
);
print_r($user); // i will print so you can figure out how to use this for your needs
$connect = null; //close connection
}
function getall($connect, $table, $values, $conditions = null, $limit = null, $ascdesc = null){
$values_str = "";
foreach($values as $key => $value){
$values_str .= $value . ", ";
}
$cond_str = "";
$hascond = false;
if($conditions != null){
$hascond = true;
foreach($conditions as $key => $value){
$cond_str .= $key . "='" . $value . "' AND ";
}
$cond_str = rtrim($cond_str, " AND ");
}
$values_str = rtrim($values_str, ", ");
$cond_str = " WHERE (" . $cond_str . ")";
$orderby = "";
$hasorder = false;
if($ascdesc != null){
$hasorder = true;
foreach($ascdesc as $key => $value){
$orderby = " ORDER BY " . $value . " " . $key;
break;
}
}
$sql = "SELECT " . $values_str . " FROM " . $table . " " . (($hascond)? $cond_str: "") . (($hasorder)? $orderby: "") . (($limit)? " LIMIT " . $limit: "");
//echo $sql;
$sql_prep = $connect->prepare($sql);
$sql_prep->execute();
return $result = $sql_prep->fetchAll(PDO::FETCH_ASSOC);
}
?>
When a user clicks the forgot password have them type in their username and email a link to the email on file with the associated user:
http://www.example.com/forgot_password.php?username=John&key=0cc175b9c0f1b6a831c399e269772661
Side note
It is Highly insecure to store passwords without hashing (many call this encryption but hashing and Encryption are entirely different) I suggest you store your passwords using password_hash read more at: http://php.net/manual/en/function.password-hash.php
I advise making the user change their password once they are authorized on the forgot_password.php script.
Your question asked how to send an email.
In order to send emails from your server you need to make sure your apache settings are configured correctly. Here is a post on stackoverflow that addresses this locally: send mail from local apache server
Once your configuration is set up correctly you can run this php function:
function send_email($subject, $msg, $to, $from){
$from - strip_tags($from);
$to = strip_tags($to);
$message = $msg;
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $from . "\r\n";
$headers .= "X-Confirm-Reading-To:" . $from . "\r\n";
$headers .= "Mailed-By:" . $from . "\r\n";
$headers .= "Disposition-Notification-To:" . $from . "\r\n";
$headers .= "Return-Receipt-To:" . $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if(mail( $to, $subject, $message, $headers ))
return true;
return false
}
I also want to add that if you are hoping to save a users login information this is done over the server and not on the client side. Append your form with a remember me check box. Have your PHP check if the text box is checked, if it is then store the users ID in a database table for remembered users.
You should also make PHP store at least 5 random unique hashes into a cookies, to server as a key for accessing the remembered information. Have our website check to see if the cookies exist & if they do match them up with your database table & pull the user id.