1
votes
<?
  function generateRenStr($length) {
    $character = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $rendom_str = "";
    $loopNum = $length;
    while ($loopNum--) {
      $rendom_str .= $character(mt_rand(0, strlen($character)));
    }
    return $rendom_str;
  }
?>

When I run it, web browser says

'Fatal error: Call to undefined function 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ() in C:\Bitnami\wampstack\apache2\htdocs\loginmodule\lib\codegen.php on line 7'
And '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' is NOT a function.

How can I fix it?

1
change this $rendom_str .= $character(mt_rand(0, strlen($character))); to this $rendom_str .= $character[mt_rand(0, strlen($character)-1)]; - Sahil Gulati
Try this one eval.in/863937 - Sahil Gulati

1 Answers

2
votes

The issue is here:

while ($loopNum--) {
  $rendom_str .= $character(mt_rand(0, strlen($character)));
}

$character is a variable that stores a string and you're trying to access it as a function. Wrong brackets used (), you need [].

Try this:

while ($loopNum--) {
  $rendom_str .= $character[mt_rand(0, strlen($character)-1)];
}