Added ability to change password.

Contributed by Liamxroy.
This commit is contained in:
MobiusDev 2018-03-21 19:56:56 +00:00
parent 62af8eeac1
commit 6a1834844e

View File

@ -8,7 +8,7 @@ if(empty($_SESSION['account']))
} }
$error = ""; $error = "";
if(isset($_POST['register'])) if(isset($_POST['changePassword']))
{ {
$conn = new mysqli($server_host, $db_user_name, $db_user_password, $db_database); $conn = new mysqli($server_host, $db_user_name, $db_user_password, $db_database);
// Check connection // Check connection
@ -19,42 +19,56 @@ $error = "";
exit(); exit();
} }
$account = mysqli_real_escape_string($conn, $_POST['username']); $account = mysqli_real_escape_string($conn, $_SESSION['account']);
$password = base64_encode(sha1($_POST['password'], true)); $password = base64_encode(sha1($_POST['password'], true));
$passwordOld = base64_encode(sha1($_POST['passwordOld'], true));
$email = $_POST['email'];
if($_POST['password']!=$_POST['passwordVerify']){ if($_POST['password']!=$_POST['passwordVerify']){
$error .= "Password does not match.<br>"; $error .= "Password does not match.<br>";
} }
if(mb_strlen($_POST['passwordOld'])<4 || mb_strlen($_POST['passwordOld'])>16){
if(mb_strlen($account)<4 || mb_strlen($account)>14){ $error .= "Old Password length must be 4 to 16 characters long.";
$error .= "Account length must be 4 to 14 characters long."; }
}
if(mb_strlen($_POST['password'])<4 || mb_strlen($_POST['password'])>16){ if(mb_strlen($_POST['password'])<4 || mb_strlen($_POST['password'])>16){
$error .= "Password length must be 4 to 16 characters long."; $error .= "Password length must be 4 to 16 characters long.";
} }
if($password == '')
if(mb_strlen($email)<7 || mb_strlen($email)>100){ $error = 'Enter password';
$error .= "Email length must be 7 to 100 characters long.";
}
$sql = "SELECT `login` FROM `accounts` WHERE `login`='".$account."'";
$result = $conn->query($sql);
if ($result->num_rows!=0) {
$error .= "Account already exist.<br>";
}
if($passwordOld == '')
$error = 'Enter old password';
$sql = "SELECT * FROM `accounts` WHERE `login`='".$account."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc())
{
if ($passwordOld == $row['password'])
{
$error = "";
}
else
{
$error = 'Incorrect Old password';
}
}
}
else
{
$error = 'Something went wrong [1]';
}
if(empty($error)){ if(empty($error)){
echo ($account.$password.$email); $sqlupdate = "UPDATE `accounts` SET `password`='".$password."' WHERE (`login`='".$account."')";
$sqlregister = "INSERT INTO `accounts` (`login`, `password`, `email`, `lastIP`) VALUES ('".$account."','".$password."','".$email."','".$_SERVER['REMOTE_ADDR']."')"; if ($conn->query($sqlupdate) === TRUE) {
if ($conn->query($sqlregister) === TRUE) { $error = "Password Successfuly Updated";
$error = "Account created!"; $_SESSION['password'] = $password;
sendemail($CONFIG['emailsmtp'], $CONFIG['emailuser'], $CONFIG['emailpass'], $CONFIG['emailaddress'], "L2j Mobius", "Password Change", $email, $account, $_POST['password']); header( "refresh:2;url=dashboard.php" );
header( "refresh:5;url=index.php" ); }
} else { else {
$error = "Something went wrong"; $error = "Something went wrong [2]";
} }
} }
@ -87,7 +101,7 @@ $error = "";
$(document).ready(function(){ $(document).ready(function(){
$('#register').submit(function() { $('#changePassword').submit(function() {
if($('#password').val() != $('#passwordVerify').val()){ if($('#password').val() != $('#passwordVerify').val()){
alert("Please re-enter confirm password"); alert("Please re-enter confirm password");
@ -98,9 +112,8 @@ $error = "";
function clear_form() function clear_form()
{ {
$("#email").val('');
$("#username").val('');
$("#password").val(''); $("#password").val('');
$("#passwordOld").val('');
$("#passwordVerify").val(''); $("#passwordVerify").val('');
} }
}); });
@ -176,6 +189,13 @@ $error = "";
<div> <div>
<form id="changePassword" method="post"> <form id="changePassword" method="post">
<div class="form-group">
<input class="form-control" data-error="Old Password is required." id="passwordOld" name="passwordOld" placeholder="Please enter your Old Password" required="required" type="password" value="<?php if(isset($_POST['passwordOld'])) echo $_POST['passwordOld'] ?>">
<div class="help-block with-errors">
</div>
</div>
<div class="form-group"> <div class="form-group">
<input class="form-control" data-error="Password is required." id="password" name="password" placeholder="Please enter your New Password" required="required" type="password" value="<?php if(isset($_POST['password'])) echo $_POST['password'] ?>"> <input class="form-control" data-error="Password is required." id="password" name="password" placeholder="Please enter your New Password" required="required" type="password" value="<?php if(isset($_POST['password'])) echo $_POST['password'] ?>">
@ -191,7 +211,7 @@ $error = "";
<div class="help-block with-errors"> <div class="help-block with-errors">
</div> </div>
</div> </div>
<input class="form-btn btn" id="submit" name="register" type="submit" value="REGISTER"> <input class="form-btn btn" id="submit" name="changePassword" type="submit" value="Change Password">
</form> </form>
</div> </div>
@ -211,7 +231,7 @@ $error = "";
<a href="http://l2jmobius.com"><img alt="" src="images/l2jmobius.png" title=""></a> <a href="http://l2jmobius.com"><img alt="" src="images/l2jmobius.png" title=""></a>
</div> </div>
<script> <script>
var url = 'index.php'; var url = 'dashboard.php';
</script> </script>
<script src="js/jquery.cookie.min.js"> <script src="js/jquery.cookie.min.js">
</script> </script>