• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

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...
            



      



0
miloudi
Asked:
miloudi
  • 2
1 Solution
 
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
 
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now