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 6626867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:56:42+00:00 2026-05-25T21:56:42+00:00

I used this hash function for a while (got it from the internet). The

  • 0

I used this hash function for a while (got it from the internet). The thing is it used to work immediately but now it is complaining about a paramter. The following is the code:

function generateHash($plainText, $salt = null)
{

    if ($salt === null)
    {
        $salt = substr(md5(uniqid(rand(), true)), 0, SALT_LENGTH);
    }
    else
    {
        $salt = substr($salt, 0, SALT_LENGTH);
    }
    return $salt . sha1($salt . $plainText);
}

So I would use this code in the method call:

validateUserInput($userid, $pass);

and validateUserInput is:

function validateUserInput($username, $password)
{
    //$username = mysql_real_escape_string($username);
    //$password = mysql_real_escape_string($password);

    if(!$username || !$password)
    {
        //$errors['credentials'] = 'Missing Credentials!';
        //$_SESSION['errors_array'] = $errors;
        //echo $errors['credentials'];
        header("LOCATION:XXXXXXX.php");
    }

    $local_salt = generateHash($password);
    //echo $local_salt;
    $groupid;

    if($username != null && $password !=null)
    {   
        connectToServer();
        $result = mysql_query("SELECT * FROM users WHERE hashkey = '{$local_salt}'");

        while($row_access = mysql_fetch_array($result))
        {
            $groupid = $row_access['groupid'];
        }
        if(!isset($result))
        {
            $errors['not_found_user'] = 'No Users Found with Provided Credentials!';
            //$_SESSION['errors_array'] = $errors;
            $userfound = 0;
            $_SESSION['user_available'] = $userfound;
        }elseif(isset($result)){
            $_SESSION['user_logged'] = array('username' => $username, 'password' => $password, 'salt' => $local_salt, 'groupid' => $groupid);
            $userfound = 1;
            //echo "stored";
            $_SESSION['user_available'] = $userfound;
        }       
    }
}

finally the error is:

Warning: substr() expects parameter 3 to be long, string given in /home/XXXX.php on line 64

This is pointing to the function generateHash()

  • 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-05-25T21:56:42+00:00Added an answer on May 25, 2026 at 9:56 pm

    The error itself tells you everything. The constant SALT_LENGTH is not a long. I suspect it’s not defined at all, so PHP converts the bare string to a string ("SALT_LENGTH") and passes that to substr(), which complains.

    That being said… This code is dangerously wrong:

    1. if(!isset($result)): Really? This condition will always be false because $result will always be set (unless you run into a problem with mysql_query(), but that doesn’t tell you anything about the valididty of the login). And since mysql_query() never returns null, no logins will ever be rejected.

    2. This query:

      SELECT * FROM users WHERE hashkey = '{$local_salt}'
      

      Is invalid. $local_salt = generateHash($password);. From the generateHash function, if a salt is not given, one will be randomly created for you. Thus, every call to generateHash will generate a new hash, which means it can’t be compared to anything in the database.

    On the basis of the two (very) egregious mistakes above, I would throw away this piece of code for good.


    The correct way to check for a valid hash when a salt is used is something like:

    $_SESSION['user_logged'] = null;
    
    // fetch hashed pw from db, where username is the submitted username
    $result = mysqli_query("SELECT hash FROM users WHERE username = '{$username}'");
    
    if ($result->num_rows != 0) 
    {
         $row = $result->fetch_assoc();    
         $hash = $row['hash'];
    
         $salt = substr($hash, 0, SALT_LENGTH); // extract salt 
    
         if (generateHash($password, $salt) == $hash) 
         {
             // login successful. 
             $_SESSION['user_logged'] = $username; // don't store passwords here
         }
    }
    
    // if $_SESSION['user_logged'] is not set, the login failed
    if (!isset($_SESSION['user_logged'])) 
    {
        // you *don't* want to tell people which one (login or pw) is invalid
        echo 'Invalid login or password';
    }
    

    Note: It’s very important that the SALT_LENGTH is at most 32, or this won’t work because of the way generateHash() is implemented.

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

Sidebar

Related Questions

I have used this code for extracting urls from web page.But in the line
I used this guide on Apple's website to enable PHP on my computer but
In my project i used this below function public function createIconMenu():NativeMenu{ var iconMenu:NativeMenu =
Has anyone used this plugin? I don't know if my setup is right, but
One of our developers used this call: TzSpecificLocalTimeToSystemTime() but unfortunately we cannot keep it
Take a commonly used binary hash function - for example, SHA-256. As the name
I have used unsalted md5/sha1 for long time, but as this method isn't really
For this program i have only used field separators from data files in shell
Recently I refactored the code of a 3rd party hash function from C++ to
I'm thinking about a hash function for arbitrary Java-objects for exercise means. The naive

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.