<?php
	$username = $_POST['username'];
	$oldpw = $_POST['oldPW'];
	$newpw = $_POST['newPW'];
	$newconfirm = $_POST['newPWC'];
	$filename = "UserDB/" . $username . ".txt";
	
	//check old pw
	$file = fopen($filename,"r");
		$readed = fread($file,filesize($filename));
		fclose($file);
	$rearray = explode("\r\n",$readed);
	$hashf = $rearray[0];
	$money = $rearray[1];
	$transferlast = $rearray[3];
	
	$confirm = password_verify($oldpw, $hashf);
	
	if($newpw == $newconfirm && $confirm == true) {
		$newHash = password_hash($newconfirm, PASSWORD_DEFAULT);
		
		$content = $newHash . "\r\n" . $money . "\r\n" . $_SERVER['REMOTE_ADDR'] . "\r\n" . $transferlast;
		
		$opn = fopen($filename,"w");
		$write = $content;
		fputs($opn,$write);
		fclose($opn);
		
		echo "Change Succes!";
	}
	else {
		echo "Confirm password not match or wrong password!";
	}
?>