Jump to content

Creating A Random String Of 16 Characters And Set As A Global Variable.


BrettHartel

Recommended Posts

Thank you for taking the time to help me.

 

I am trying to generate a string of sixteen characters that can look like 0000000000000000 to 9999999999999999 and set it as the global variable of $User_ID

 

Here is the part of the code I am having trouble with:

function genRandomString() {
 global $User_ID;
 $length = 16;
 $characters = "0123456789";
for ($p = 0; $p < $length; $p++) {
	 $User_ID .= $characters[mt_rand(0, strlen($characters))];
 } }

 

Here is my whole code:

<?php
$con = mysql_connect("localhost","******","******");
if (!$con){ die('Could not connect: . mysql_error()'); }
mysql_select_db("******", $con);
echo "Connected to: " . $con;
$eMail="$_POST[eMail]";$User_ID=""; echo "<br>My eMail: " . $eMail;
$eMailResult = mysql_query("SELECT * FROM eMail
WHERE eMail='$eMail'");
if (mysql_num_rows($eMailResult) == 0){
echo "<br>eMail is Unique"; }
else { echo "eMail is NOT Unique<br>";}
function genRandomString() {
 global $User_ID;
 $length = 16;
 $characters = "0123456789";
for ($p = 0; $p < $length; $p++) {
	 $User_ID .= $characters[mt_rand(0, strlen($characters))];
 } }
echo "<br>User ID: " . $User_ID;
mysql_close($con);
?>

Thank you Muddy_Funster but I figured it out. I can use this:

$User_ID=rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);

This is such a bad, bad idea...

 

Use MySQL's AUTO_INCREMENT for user IDs, and if you want a random and unique identifier for users use either PHP's uniqid () or MySQL's UUID() functions.

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.