<?php
session_start();
 if (!file_exists('users/'.$_SESSION['username']. '.xml')) {
    header('Location:index.php');
     die;
 }
$error = false;
if(isset($_POST['change'])) {
    $old = md5 ($_POST['o_password']);
    $new = md5 ($_POST['n_password']);
    $c_new = md5 ($_POST['c_n_password']);
    
    $xml = new SimpleXMLElement('users/' . $_SESSION['username'] . '.xml' , 0 , true);
    if($old == $xml->password){
        if($new == $c_new){
            $xml->password = $new;
            $xml->asXML('users/'.$_SESSION['username']. '.xml');
            header('Location: logout.php');
            die;
        }   
    }
    
    $error = true;
}


?>
<html>
<?php include 'header.php';?>
    <div class="content">

            <div class="content2">
                
<h1>Change Password</h1>
        <form method="post" action="">
            <?php
            if($error) {
                echo '<p>Some of the passwords don\'t match</p>';
            }
            ?>
            <p>Old Password:<input type="password" name="o_password"/></p>
            <p>New Password:<input type="password" name="n_password"/></p>
            <p>Confirm Password:<input type="password" name="c_n_password"/></p>
            <p><input type="submit" name="change" value="change password"/></p>
        </form>
        <hr/>
        <a href="index.php">User home</a>
        </div>

    </div>
    
<?php include 'sidebar.php';?>   
    </div> 
    </body>
</html>