Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8821815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:56:48+00:00 2026-06-14T05:56:48+00:00

I have registered a new user and saved the username, password & salt in

  • 0

I have registered a new user and saved the username, password & salt in the DB using the following hashing method:

    if(isset($_POST['register']))
    {
    $password = $_POST['password']

    function sanitize($data)
    {
        $data=trim($data);
        $data=htmlspecialchars($data);
        $data=mysql_real_escape_string($data);
        return $data;
    }

    $password = sanitize($password);

    function createSalt()
    {
        $salt = bin2hex(mcrypt_create_iv(32,MYCRYPT_DEV_URANDOM));
        $hash = hash("sha256", $salt);
        $final = $salt.$hash;
        return $final;
    }

    $hashedPassword = hash("sha256", $password);
    $salt = createSalt();
    $hashedPassword = hash("sha256", $hashedPassword.$salt);

    $query = sprintf("INSERT INTO users(username, password, salt) VALUES('%s','%s','%s')",$username, $hashedPassword, $salt);
}

And Later while trying the login.php, I am entering the same password which I saved during registration and using the below code to check if the entered password is the same as the one in the DB

if(isset($_POST['login']]))
{
            $password = $_POST['password']

    function sanitize($data)
    {
        $data=trim($data);
        $data=htmlspecialchars($data);
        $data=mysql_real_escape_string($data);
        return $data;
    }

    function validateUser()
    {
        session_regenerate_id (); //this is a security measure
        $_SESSION['valid'] = 1;
        $_SESSION['username'] = $username;
    }

    $password = sanitize($password);

    $query = sprintf("SELECT * FROM users WHERE username = '%s'",$username);
    $sql = mysql_query($query);
    $count = mysql_num_rows($sql);

    $row = mysql_fetch_array($sql);

    if($count<1)
    {
        echo $count;
        unset($_POST['login']);
        header("location:login.php");
        exit;
    }

    $hash = hash("sha256", $password);
    $salt = $row['salt'];
    $hash = hash("sha256",$hash.$salt);

    echo $hash."<br />".$row['password']."<br /><br />";

    if($hash != $row['password'])
    {
        unset($_POST['login']);
        header("location:login.php");
        exit;   
    }
    else
    {
        validateUser();
        unset($_POST['login']);
        header("location:index.php");
        exit;   
    }
}

These passwords are not getting matched.
Kindly let me know what’s wrong in this code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:56:49+00:00Added an answer on June 14, 2026 at 5:56 am

    There is nothing wrong with your code.

    the salt value stored in the database is truncated because the varchar value is low increase the varchar value of your salt column to 200-300 something and than try this.. it will run fine.

    I facepalmed when I found out this was screwing the result..

    Dins

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have modified my application, so that when a new user is registered, it's
In views.py I have the following view which gets called when a new user
I am using from social_auth.signals import socialauth_registered for new registered user in my project.
I have a user registration Form. If a new User register their account in
I have registered a component like this in my Global.asax.cs: ContainerBuilder builder = new
I have developed a android application using phonegap. I have registered certain recievers which
I have an Android application which needs username and password to login. I need
I have the following table in the database: id, username, email, old_ip, current_ip Everytime
I have a page that lets an admin user register a new user, by
I have a new project on django, in which im using Grappelli and filebrowser,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.