// check to see if user is signed in
session_start();
// make connection to database
include 'TPGP_db.php';
// session_checker();
// determine whether to display form
if(!$_POST['visited']){
include 'TPGP_ChangePassword.htm';
exit();
} elseif($_POST['visited'] == "visited" && (!$_POST['old_pw'] || !$_POST['new_pw'] || !$_POST['username'])){
echo "Please fill in all fields!";
include 'TPGP_ChangePassword.htm';
exit();
} elseif($_POST['visited'] == "visited" && isset($_POST['old_pw']) && isset($_POST['new_pw']) && isset($_POST['username'])) {
// rename post vars into easier to handle ones
$uname = $_POST['username'];
$oldpw = $_POST['old_pw'];
$newpw = $_POST['new_pw'];
changepw($uname, $oldpw, $newpw);
}
/* ********************************************************************************************
* All the simple stuff is taken care of, it's time to write this function! 11/17/03
* Everything written by Kevin Martin, based off of phpfreaks's membership area tutorial
* ( http://www.phpfreaks.com/tutorials/40/0.php )
*
* If you choose to use this function, this comment-block will be neccessary on your site
*
* Function changes randomly generated password into user-specific one
*
* Special thanks to Daeken and phpfreaks.com
************************************************************************************************/
function changepw($usename, $oldpw, $newpw){
// ecrypt passwords
$newpw = md5($newpw);
$oldpw = md5($oldpw);
// rename session variables for ease of use
// $email = $_SESSION['email_address'];
// validate old password against DB
$query = "SELECT * FROM users WHERE username='$usename' AND password='$oldpw'";
$result = mysql_query($query);
$rows = mysql_num_rows($result);
if($rows != 1){
// error handling
echo "Error!
You're current password does not match actual entry in the database, try again!";
include 'TPGP_ChangePassword.htm';
exit();
} else {
// everything is going smoothly, update password
$query = "UPDATE users SET password='$newpw' WHERE username='$usename'";
$result = mysql_query($query);
if(!$result){
// theres been a mix up, error handling
echo "Error!
Your password can not be changed. Please Contact Turning Point at Granita Park.";
exit();
} else {
// display the good stuff!
echo 'Your Password has been successfully changed!
Click here to try your new password!';
exit();
}
}
}
?>