Jump to content

PHP Error - "Uninitialized string offset: 36"


jeradb

Recommended Posts

Hi, I am new to the forums. I am having an issue with a snippet of code that generates random strings.  I have already searched the forums, but the fix that I found is already in my code.

I am still receiving the error though and was wondering if there was something else going on that I didn't see.  Here is my code:

 

        $Data = 'Ahmet'.md5(mt_rand(0, 123456789));

$length = 30;
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$string = '';
$string2 = '';
for ($a = 0; $a < $length; $a++)
{
	$pos = mt_rand(0, strlen($chars)-1);
	$string .= $chars{$pos};

	$pos2 = mt_rand(0, (strlen($Data))-1);
	$string2 .= $chars{$pos2};
}

 

I appreciate any help on this, as it is cluttering my system log table.

Thanks.

have you tried something more conventional like:

for ($a = 0; $a < $length; $a++)
{
	$pos = mt_rand(0, strlen($chars)-1);
	$string .= substr($chars, $pos, 1);

	$pos2 = mt_rand(0, (strlen($Data))-1);
	$string2 .= substr($chars, $pos2, 1);
}

the other thing I will say is that your $Data is likely a good bit longer than your $chars, so watch out for that returning a $pos2 that is outside the range of $chars.

Sorry for the double post, but I realized what I did wrong. I used $chars for $string2 instead of $Data.  Once I changed it, everything worked fine.

 

for ($a = 0; $a < $length; $a++)
			{
				$pos = mt_rand(0, strlen($chars)-1);
				$string .= $chars{$pos};

				$pos2 = mt_rand(0, strlen($Data)-1);
				$string2 .= $Data{$pos2};
			}

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.