Here's a snippet of my code:

<?
include("include/session.php");
//include("forum/global.php");

$con = mysql_connect($dbhost,$dbuser,$dbpass);

$time = time();
$date = date("Y-n-j H:i:s");
$ip = $_SERVER['REMOTE_ADDR'];
$email = $_POST['email'];
$lastname = ucwords($_POST['lastname']);
$firstname = ucwords($_POST['firstname']);
$firstinitial = $firstname[0];
$username = $firstinitial."_".$lastname;
$pass = $_POST['password'];

// MYBB

function generate_salt()
{
    return random_str(8);
}

function salt_password($password, $salt)
{
    return md5(md5($salt).$password);
}

$md5pass = md5($pass);
$salt = generate_salt();
$salted_pass = salt_password($md5pass, $salt);

$loginkey = random_str(50);

I get this error:

Fatal error: Call to undefined function random_str() in /home/myusername/public_html/thefile.php on line 20

Line 20 is the return random_str() line.
Can someone help me solve my problem please?

Dani AI

Generated

Short answer: your script calls a function named random_str() that does not exist. is right — either define that function or include the file that defines it. 's return random() is incorrect: PHP has no random() function (there are rand(), mt_rand() and, on newer PHP, random_int() / random_bytes()), and those choices have different security properties.

Here is a simple, useable implementation that picks the best available source of randomness and falls back to a safe option:

function random_str($length = 8)
{
    $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $max = strlen($chars) - 1;
    $out = '';

    if (function_exists('random_int')) {
        for ($i = 0; $i < $length; $i++) {
            $out .= $chars[random_int(0, $max)];
        }
        return $out;
    }

    if (function_exists('openssl_random_pseudo_bytes')) {
        $bytes = openssl_random_pseudo_bytes($length);
        if ($bytes !== false) {
            return substr(bin2hex($bytes), 0, $length);
        }
    }

    for ($i = 0; $i < $length; $i++) {
        $out .= $chars[mt_rand(0, $max)];
    }
    return $out;
}

Security note: generating a salt manually and then hashing with MD5 is not recommended. Let PHP handle hashing with password_hash() (bcrypt/argon2) and verify with password_verify() — it manages salts and iterations correctly. If you must match another system (for example an existing forum), check that system's exact hashing routine or use its API so user authentication stays compatible.

Practical checks: define random_str() before it is first used or put it in an included file (make sure the include is not commented out), prefer <?php tags for portability, and test with a tiny script to confirm the function exists before integrating into registration logic.

Recommended Answers

All 2 Replies

If you check line 21 of the code you pasted above, there is a call to the function random_str(). Where did you define this function ? By defining, it means where is the implementation of the function ? eg

function random_str($iLength)
{
  // code for generating a string containing $iLength random characters 
  // return the generated string
}

So, define your function somewhere in that file or perhaps in the global.php (which is commented out ?!?) then your error should vanish

Meaning random_str() inside the method generate_salt() is not defined

just write

return random()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.