How to Manually Insert a Group of Users Into an SMF Forum Using PHP
This Tutorial assumes you are familiar with my post: How to Manually Insert a User Into an SMF Forum Using PHP.
I recently faced the challenge of moving 800 or so bands from a database into an SMF forum. I faced a couple of problems when making the move that I didn’t cover in the first post:
- The band names weren’t guaranteed to be safe for a character by character move into the SMF database. So some kind of name modifying function had to be employed.
- The bands don’t have passwords. So some type of password generation needs to be employed.
The steps I went about to register all of the bands in the forum went a little like this:
Open the Band Database
for each (Band){
Generate Password
Rename Band with "safe" name
Grab and generate information needed to insert the user
Insert Band into smf_members
Send Band an email with the password
}
Information I had available from the band database that was useful included: bandName, bandEmail, and bandWebsite
Here is the password generation function I used (blatently taken from totallyphp.co.uk):
function createRandomPassword() {
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
srand((double)microtime()*1000000);
$i = 0;
$pass = ” ;
while ($i <= 7) {$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;}
return $pass;
}
The password created here is relatively weak, but it was good enough for my purposes. Any password generation function would work fine here. I chose this one because it would be easy for members to remember if they chose not to change it.
How to Do It:
//First connect to the band database
$username = "bandUsername ";
$host = "bandHost";
$mypassword = "bandPassword";
$db_name = "bandDatabase";
mysql_connect("$host", "$username", "$mypassword")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");
//Now grab all of the bands from the database
$query = "SELECT * FROM allBands ";
$bands = mysql_query($query) or die ("Config Error 2222b");
$userRows = mysql_num_rows($bands);
//Now lets connect to the smf db
$host="smfhost"; // Host name
$username="smfuser; // Mysql username
$mypassword="smfpassword"; // Mysql password
$db_name="smfdatabase"; // Database name
mysql_connect("$host", "$username", "$mypassword")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");
//$vaild_chars is going to be the array of allowed characters for usernames. I decided to only allow letters and numbers. There are other characters that could be used that wouldn’t cause a problem, but for style reasons I decided to go with only letters and numbers
$valid_chars = "a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9";
$valid_chars = explode(" ",$valid_chars);
//Now we need to loop through each band
for ($i = 0; $i < $userRows; $i++){
if ($row = mysql_fetch_assoc($bands)){
//Extract the band information
extract($row);
//Create a password for the band
$realPassword = createRandomPassword();
//I choose to echo the bandname and password as part of my debugging and so I can see the function as it works
echo("<br/>$bandName – $realPassword");
$newBand = "";
//For each letter in the band name we are going to look to see if it matches a valid character from $valid_chars. It it does not we are going to get rid of it and truncate the band name
for ($j = 0; $j < strlen($bandName); $j++){$temp = str_replace($valid_chars, "X", strtolower($band[$j]));
if ($temp == "X")$newBand = $newBand.$band[$j];
}
//I echo the new name for the same reason I echo the old name and password
echo(" – $newBand");
//We have the new Band name…. now let’s start buliding the info we need to insert in the db…
$memberName = $newBand;
$realName = $newBand; //I set the real name and member name the same. The bands can change it if they want
$emailAddress = $bandEmail;
$websiteUrl = $bandWebsite; //Since I have the bands’ websites might as well enter them
$websiteTitle = $newBand;
$is_activated = 1;
$ID_POST_GROUP = 4;
$password = sha1(strtolower($memberName).$realPassword); //Password must be encrypted//Make sure their isn’t a member with the same name in the SMF database. We don’t want to accidentally enter anyone twice
$query = "SELECT * FROM smf_members WHERE memberName = ‘$memberName’";
$result = mysql_query($query) or die ("Config Error 2222343242b");
$nrows = mysql_num_rows($result);//If the member name is found do nothing
if ($nrows>0){}else {
//If the user isn’t already signed up for the forums, do so and send an email….
$query = "INSERT INTO smf_members(memberName, realName, emailAddress, is_activated, ID_POST_GROUP, passwd, websiteUrl, websiteTitle ) VALUES(’$memberName’, ‘$realName’, ‘$emailAddress’, ‘$is_activated’, ‘$ID_POST_GROUP’, ‘$password’, ‘$websiteUrl’, ‘$websiteTitle’)";$result = mysql_query($query) or die ("Config Error 2232 ");
//Now we need to send the band an email
$to=$emailAddress;
$subject="Your forum username and password ";// From
$header="from: Me <me@mysite.com>";// Your message
$message.="Your login and password are listed below. You can change either at anytime. If you don’t want to participate in our forums, that’s no problem. Just never log in and it will be like nothing ever happened.\r\n";
$message.="Username: $memberName Password: $realPassword \r\n";// send email
$sentmail = mail($to,$subject,$message,$header);
if ($sentmail)echo("- YES!");
//This is the last part of our echo. The " -YES!" will only be printed if an email is sent. So a full printout line will look something like this:
//Adam Strife – 4s6vsxag – AdamStrife – YES!}
}
}








































