<html>
<! ChangePassword.php                                         >
<! Chapter 13, Oracle Database XE PHP Web Programming         >
<! by Michael McLaughlin                                      >
<!                                                            >
<! This script demonstrates how to you can change a user      >
<! password from PHP program.                            >
<head>
<title>
  Chapter 13: ChangePassword.php
</title>
<style>
.e {background-color: #ccccff; font-weight: bold; color: #000000;}
.v {background-color: #cccccc; color: #000000;}
</style>
</head>
<body>
<?php
  // Declare input variables.
  (isset($_GET['uname'])) ? $u_name = $_GET['uname']
                          : $u_name = "plsql";
  // Declare input variables.
  (isset($_GET['opasswd'])) ? $o_passwd = $_GET['opasswd']
                            : $o_passwd = "plsql";
  // Declare input variables.
  (isset($_GET['npasswd'])) ? $n_passwd = $_GET['npasswd']
                            : $n_passwd = "oracle";

  // Change password.
  change_password($u_name,$o_passwd,$n_passwd);

  function change_password($u_name,$o_passwd,$n_passwd)
  {
    // Connect to database.
    if ($c = @oci_connect($u_name,$o_passwd, "xe"))
    {
      print oci_server_version($c)."<br>";

      // Change the user password.
      if (oci_password_change($c,$u_name,$o_passwd,$n_passwd))
        print get_params($u_name,$o_passwd,$n_passwd);
      else
        print "Password Unchanged.<br>";

      // Close connection.
      oci_close($c);
    }
    else
    {
      // Assign the OCI error and format double and single quotes.
      $errorMessage = oci_error();
      print htmlentities($errorMessage['message'])."<br />";
    }
  }

  // Return an HTML table of user, old password and new password.
  function get_params($u_name,$o_passwd,$n_passwd)
  {
    $out  = '<table border="1" cellpadding="0" cellspacing="0">';
    $out .= '<tr><td class="e" width="125">User/Schema</td>';
    $out .= '<td class="v" width="125">'.$u_name.'</td></tr>';
    $out .= '<tr><td class="e">Old Password</td>';
    $out .= '<td class="v">'.$o_passwd.'</td></tr>';
    $out .= '<tr><td class="e">New Password</td>';
    $out .= '<td class="v">'.$n_passwd.'</td></tr>';
    $out .= '</table>';

    // Return connect elements.
    return $out;
  }
?>
</body>
</html>
