Jump to content

[SOLVED] base64_decode help


pcw

Recommended Posts

Hi, I am writing a registration and login script.  The script successfully encodes the password and writes the result to the mysql database.

 

When it comes to the logging in part I am using base64_decode to check the password matches that as to what is listed in the database, but it just states that that login was not successful. It worked fine until I changed the string to using base64_decode.

 

Any help is much appreciated.

 

function login_chk() {

if (isset($_POST['submit'])) {

   $db = "moveitho_sitebuilder";

  mysql_connect('localhost', 'moveitho_paul', 'test') or die(mysql_error());
  mysql_select_db( $db) or die(mysql_error());
  
   $username = mysql_real_escape_string($_POST['username']);
  $password_decoded = mysql_real_escape_string(base64_decode($_POST['password']));

  if ($result = mysql_query("SELECT username, password, verified FROM users WHERE username='$username' AND password='$password_decoded' AND verified='yes'")) {
    if (mysql_num_rows($result) > 0) {
          echo "Login successful";
             }else {
          echo "Login not successful.";
         
    }
}else{

    echo "SQL Error: " . mysql_error();
}
}
};

Link to comment
https://forums.phpfreaks.com/topic/147003-solved-base64_decode-help/
Share on other sites

Ok, here is the code that writes the data to the database. However I think I know what I am doing wrong. I should be comparing the password upon login to to that as to what has been decoded from the password table in the database.

 

It works if:

 

Password on registration - Password encoded - Writes encoded password to db

 

Password on login - Password encoded - Reads encoded password from db and gets a match.

 

However the way I have it at the mo:

 

Password on registration - Password encoded - Writes encoded password to db

 

Password on login - Password decoded - Reads encoded password from db and does not match as the password in the database is still encoded.

 

Here is the code for writing to the db

 

function db_add_user() {
$db = "moveitho_sitebuilder";
$link = mysql_pconnect( "localhost", "moveitho_paul", "test" );
if ( ! $link ) {
$dberror = mysql_error();
return false;
}
if ( ! mysql_select_db( $db, $link ) ) {
$dberror = mysql_error();
return false;
}
$password = base64_encode($_POST['password']);     
$query = "INSERT INTO users ( gen_id, username, password, first_name, last_name, email )
values('$_POST[gen_id]', '$_POST[username]', '$password', '$_POST[first_name]', '$_POST[last_name]', '$_POST[email]')";
if ( ! mysql_query( $query, $link ) ) {
$dberror = mysql_error();
return false;
}
return true;
};

If you encoded it when you originally stored it, you cannot match a decoded password against an encoded password.

 

$password_decoded = mysql_real_escape_string(base64_decode($_POST['password']));

 

change this to base64_encode and see if it works.

First of all....when it comes to encoding passwords, the term base64 shouldn't be in the picture. you should be using a one-way encryption like md5, sha1, etc.

 

so, in your registration code, use crypt() instead. this will use the best encryption method your version of php supports.

 

then, change your login code to:

<?php
function login_chk() {
  if (isset($_POST['submit'])) {
    $db = "moveitho_sitebuilder";
    mysql_connect('localhost', 'moveitho_paul', 'test') or die(mysql_error());
    mysql_select_db( $db) or die(mysql_error());

    $username = mysql_real_escape_string($_POST['username']);
    if ($result = mysql_query("SELECT username, password, verified FROM users WHERE username='$username' AND verified='yes'")) {
      if (mysql_num_rows($result) === 1) {
        $info = mysql_fetch_array($result);
        if(!strcmp($info['password'],crypt($_POST['password'],$info['password']))){
          echo "Login successful";
        }else {
          echo "Login not successful.";
        }
      }else{
        echo "User not found";
      }
    }else{
      echo "SQL Error: " . mysql_error();
    }
  }
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.