How to reset password with email in php and mysqli
step one create database
-- Database: `raj`
--
-- --------------------------------------------------------
--
-- Struktur dari tabel `user`
--
CREATE TABLE `user` (
`id` int(100) NOT NULL,
`username` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
`token` varchar(1000) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data untuk tabel `user`
--
INSERT INTO `user` (`id`, `username`, `password`, `email`, `token`) VALUES
(1, 'hens3159', 'lukmana12345', 'hens3159@gmail.com', 'ijh8aqt6w7');
step one create database
-- Database: `raj`
--
-- --------------------------------------------------------
--
-- Struktur dari tabel `user`
--
CREATE TABLE `user` (
`id` int(100) NOT NULL,
`username` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
`token` varchar(1000) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data untuk tabel `user`
--
INSERT INTO `user` (`id`, `username`, `password`, `email`, `token`) VALUES
(1, 'hens3159', 'lukmana12345', 'hens3159@gmail.com', 'ijh8aqt6w7');
step 2:db.php database connection
<?php
$host = '127.0.0.1';
$user = 'root';
$pass = '';
$db = 'raj';
$link = mysqli_connect ($host, $user, $pass, $db) or die (mysqli_error()); //die digunakan untuk memberhentikan syntax sampai disini
?>
<?php
session_start();
function result ($query) {
global $link;
if ($result = mysqli_query($link, $query) or die ('gagal menampilkan data')){
return $result;
}
}
function run($query) {
global $link;
if (mysqli_query ($link, $query)) return true;
else return false;
}
function user($username) {
$query = "SELECT * FROM user WHERE username='$username'";
return result ($query);
}
function update_token($code,$username) {
$query = "UPDATE user SET token='$code' WHERE username='$username'";
return run ($query);
}
function update_pass($konfir_pass,$username) {
$query = "UPDATE user SET password='$konfir_pass' WHERE username='$username'";
return run ($query);
}
?>
step-3:login.php
<?php
require_once 'db.php';
//check for submit
if (isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$user_db = user($username);
$row= mysqli_fetch_assoc($user_db);
//if password in form same with password in database
if ($password==$row['password'] && $username == $row['username']) {
$_SESSION['user'] = $username;
if($_SESSION['user']==$username){
header ('location:home.php');
}else {
echo "login gagal";
}
}else {
echo "your password is wrong";
}
}
?>
<h3> form login </h3>
<form action="" method="post">
<label>username</label><br>
<input type="text" name="username" placeholder="username"><br>
<label>password</label><br>
<input type="text" name="password" placeholder="passwrod"><br>
<p><a href="forgot.php">forgot password</a></p>
<input type="submit" name="submit"><br>
</form>
step4: home.php
<?php
//connect to db.php
require_once 'db.php';
if (!$_SESSION['user']) {
header ('location:login.php');
}
//check submit and send email
if (isset($_POST['submit'])) {
$tujuan = $_POST['tujuan'];
$judul = $_POST['judul'];
$isi = $_POST['isi'];
$headers = "From: chikennotes@gmail.com" . "\r\n";
mail($tujuan,$judul,$isi,$headers);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Send Email</title>
</head>
<body>
<h3> Send Email </h3>
<form action="" method="post">
<label>To</label><br>
<input type="text" name="tujuan" placeholder="to"><br>
<label>Title</label><br>
<input type="text" name="judul" placeholder="title"><br>
<label>Content</label><br>
<input type="text" name="isi" placeholder="content"><br>
<input type="submit" name="submit">
</form>
</body>
</html>
step5:forgot.php
<?php
require_once 'db.php';
//check submit
if (isset($_POST['submit'])) {
$username = $_POST['username'];
$email = $_POST['email'];
$db = user($username);
$jumlah = mysqli_num_rows($db);
//check is there username in database
if ($jumlah !=0) {
while ($row=mysqli_fetch_assoc($db)){
$db_email = $row['email'];
}
//check input email similiar with email in database
if ($email==$db_email){
// make random code
$code = '123456789qazwsxedcrfvtgbyhnujmikolp';
$code = str_shuffle($code);
$code = substr($code,0, 10);
// for send token
$alamat = "http://localhost/coba2/update.php?code=$code&username=$username";
$to = $db_email;
$judul = "passwrod reset";
$isi = "this is automatic email, dont repply. For reset your password please click this link ".$alamat;
$headers = "From: chikennotes@gmail.com" . "\r\n";
mail($to,$judul,$isi,$headers);
//echo $alamat;
if (update_token($code, $username)){
echo "your password have reset";
}else {
echo "please try again";
}
}else {echo"your email didn't register";}
}else {echo"your username didn't register";}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h3>Reset Password </h3>
<form action="" method="post">
<label>username</label>
<input type="text" name="username" placeholder="username">
<label>email</label>
<input type="text" name="email" placeholder="email">
<input type="submit" name="submit">
</form>
</body>
</html>
step 6:update.php
<?php
require_once 'db.php';
$kode=$_GET['code'];
$username = $_GET['username'];
//check link
if (isset($kode) && isset($username)){
$db_user = user($username);
$row = mysqli_fetch_assoc($db_user);
$token = $row ['token'];
$db_username = $row ['username'];
//check between tokens & usernames that are in links and databases
if ($token==$kode && $db_username==$username){
//check submit
if (isset($_POST['submit'])) {
$password = $_POST['password'];
$konfir_pass = $_POST['konfir_password'];
//check password
if ($password==$konfir_pass) {
echo "password telah diupdate";
update_pass($konfir_pass, $username);
header('location:login.php');
}else {echo "password is different";}
}
}else{echo "token & username is different";}
}else{echo "link is wrong";}
?>
<!DOCTYPE html>
<html>
<head>
<title>Send Email</title>
</head>
<body>
<h3>Change your password</h3>
<form action="" method="post">
<label>password</label><br>
<input type="text" name="password" placeholder="password"><br>
<label>new password</label><br>
<input type="text" name="konfir_password" placeholder="new password"><br>
<input type="submit" name="submit">
</form>
</body>
</html>
No comments:
Post a Comment