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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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};
			}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.