allowing users to change passwords

Hello Everybody,

I have setup this file to allow users to change their passwords....
I have done almost everything but i am still missing something very small.
PHP with MySql:

<?php
session_start();

//create short variable names

$old_passwd = $_POST['old_passwd'];
$new_passwd = $_POST['new_passwd'];
$new_passwd2 = $_POST['new_passwd2'];

try {

      check_valid_user();
      
      if ($new_passwd!=$new_passwd2)
            throw new exception('Passwords entered were not the same. Not Changed!');

      //attempt update
      change_password($_SESSION['valid_user'], $old_passwd, $new_passwd);
      echo 'Password changed';
}

catch (Exception $e)

{
      echo $e->getMessage();
}

?>
<?php

function check_valid_user()
{


if (isset($_SESSION['valid_user'])) {
      echo 'Logged in as '.stripslashes($_SESSION['valid_user']).'.';
      echo '<form action="" method="post">';
      echo 'Old Password: <input type="text" name="old_passwd"><br>';
      echo 'New Password: <input type="password" name="new_passwd"><br>';
      echo 'Confirm Password: <input type="password" name="new_passwd2"><br>';
      echo '<input type="submit" name="password" value="Change Password" />';
      echo '</form>';
      
}
else
{
echo 'You are not logged in';
exit;
}

}

?>

<?php

function login($username, $password)

{

//connect to db

      $dbcn = new mysqli             
                  ("localhost","ics", "",  "ics" );
      if(mysqli_connect_errno())
                    echo "<p>Error creating database connection: </p>";
                exit;
        

      $sql = "SELECT username, password FROM user_info ";
      $sql = $sql . "WHERE username='$username' AND password='$password';";

      $result = $dbcn->query( $sql );
        if(!$result)
                 echo( "<p>Unable to query database at this time.</p>" );
                 exit();
        
        $numRows = $result->num_rows;
        if($numRows > 0)
                $row = $result->fetch_assoc();
            echo "<h2>Welcome " . $row["username"] . "</h2>";
      
}


?>




<?php

function db_connect()

{
      $result = new mysqli ("localhost","", "",  "" );
      if (!$result)
            throw new Exception('Could not connect to database server');
      else
            return $result;
}

?>



<?php

function change_password($username, $old_password, $new_password)

{

      login($username, $old_password);
      $conn = db_connect();

      $result = $conn->query("update user
                  set passwd = sha1('$new_password')
                  where username = '$username'");
      if ($result)
            echo 'Password was successfully changed';
      
            
}

?>

...Thanks for the Help..
I will be posting two more questions....because i have a deadline on this...
            



      



LVL 4
miloudiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

star_trekCommented:
In your change_password function you are using sha1 algorithm
update user set passwd = sha1('$new_password') where username = '$username'"

but when selecting you are password, i think is in clear text
SELECT username, password FROM user_info WHERE username='$username' AND password='$password'

change the SQL query to and try

SELECT username, password FROM user_info WHERE username='$username' AND password=sha1('$password')
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
miloudiAuthor Commented:
star trek,

I setup that change and i also found out that my db name was wrong, it should be user_info instead of user....
Now i seem that i have eliminate the function of password...
Like the user can enterthe site without a password.....
I beleive i have a logic error...
could you help and i would like to display the message password changed when passwd gets changed::::;;;;;
<?php
session_start();

//create short variable names

$old_passwd = $_POST['old_passwd'];
$new_passwd = $_POST['new_passwd'];
$new_passwd2 = $_POST['new_passwd2'];

try {

      check_valid_user();
      
      if ($new_passwd!=$new_passwd2)
            throw new exception('Passwords entered were not the same. Not Changed!');

      //attempt update
      change_password($_SESSION['valid_user'], $old_passwd, $new_passwd);
      echo 'Password changed';
}

catch (Exception $e)

{
      echo $e->getMessage();
}

?>


<?php

function login($username, $password)

{

//connect to db

      $dbcn = new mysqli             
                  ("localhost","ics325sp0604", "13855",  "ics325sp0604" );
      if(mysqli_connect_errno())
                    echo "<p>Error creating database connection: </p>";
                exit;
        

      $sql = "SELECT username, password FROM user_info";
      $sql = $sql . "WHERE username='$username' AND password=sha1('$password');";

      $result = $dbcn->query( $sql );
        if(!$result)
                 echo( "<p>Unable to query database at this time.</p>" );
                 exit();
        
        $numRows = $result->num_rows;
        if($numRows > 0)
                $row = $result->fetch_assoc();
            echo "<h2>Welcome " . $row["username"] . "</h2>";
      
}


?>
<?php

function check_valid_user()
{


if (isset($_SESSION['valid_user'])) {
      echo 'Logged in as '.stripslashes($_SESSION['valid_user']).'.';
      
      echo '<form action="" method="post">';
      echo 'Old Password: <input type="text" name="old_passwd"><br>';
      echo 'New Password: <input type="password" name="new_passwd"><br>';
      echo 'Confirm Password: <input type="password" name="new_passwd2"><br>';
      echo '<input type="submit" name="password" value="Change Password" />';
      echo '</form>';
      
}
else
{
echo 'You are not logged in';
exit;
}

}

?>



<?php

function db_connect()

{
      $result = new mysqli ("localhost","ics325sp0604", "13855",  "ics325sp0604" );
      if (!$result)
            throw new Exception('Could not connect to database server');
      else
            return $result;
}


function change_password($username, $old_password, $new_password)

{

      login($username, $old_password);
      $conn = db_connect();

      $result = $conn->query("update user_info
                  set passwd = sha1('$new_password')
                  where username = '$username'");
      if (!$result)
            throw new Exception('Password could not be changed');
      else
            return true;
}

?>




            



      

 
0
star_trekCommented:
change the following
try {

     check_valid_user();
     
     if ($new_passwd!=$new_passwd2)
          throw new exception('Passwords entered were not the same. Not Changed!');

     //attempt update
     change_password($_SESSION['valid_user'], $old_passwd, $new_passwd);
     echo 'Password changed';
}

catch (Exception $e)

{
     echo $e->getMessage();
}

to
if($_POST["password"] && !empty($new_passwd) && !empty($new_passwd2) && !empty(old_passwd) ) {
  try {
      if ($new_passwd!=$new_passwd2)
           throw new exception('Passwords entered were not the same. Not Changed!');

      //attempt update
      change_password($_SESSION['valid_user'], $old_passwd, $new_passwd);
      echo 'Password changed';
  }
  catch (Exception $e)
  {
     echo $e->getMessage();
 }
} else {
   check_valid_user();
}


Also in the login function brackets are missing, consider changing the following
if(!$result)
               echo( "<p>Unable to query database at this time.</p>" );
               exit();

to
if(!$result) {
               echo( "<p>Unable to query database at this time.</p>" );
               exit();
}

Also you can just use mysql instead of mysqli

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.