<?php // Plug-in 61: Add User To DB
/*
* 插件说明:
* 在数据库中添加用户
* 把一个记录添加到一个MySql数据库里。如果还没建立数据库表,则先创建一个数据库表。
* 插入成功,则返回值1.
* 插入失败。则返回-1.
* 如果这个记录已经存在,返回-2.
* 它需要以下参数:
* $table 数据表名。
* $nmax $name(用户姓名)允许的最大长度。
* $hmax $handle(用户名)允许的最大长度。
* $salt1 为了保护口令的安全,用伪随机方法生成一个字符串
* $salt2 与$salt1一起使用的第二个字符串
* $name 添加到数据库的用户的全名。
* $handle 用户名
* $pass 用户口令
* $email 用户email地址
*/
// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link
$dbhost = 'localhost'; // Normally no need to change this
$dbname = 'piphp'; // Change to your database name
$dbuser = 'root'; // Change to your database user name
$dbpass = 'xiaonan'; // Change to your database password
mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$table = 'Users';
$nmax = 32;
$hmax = 16;
$name = "George Washington";
$handle = "firstprez";
$pass = "GW022232";
$email = "george@washington.com";
$salt1 = "F^&";
$salt2 = "9*hz!";
$result = PIPHP_AddUserToDB($table, $nmax, $hmax, $salt1, $salt2,
$name, $handle, $pass, $email);
if ($result == -2) echo "The handle '$handle' already exists.";
elseif ($result == 1) echo "User '$name' successfully added.";
else echo "Failed to add user '$name'.";
echo "<br /><br />";
$result = PIPHP_AddUserToDB($table, $nmax, $hmax, $salt1, $salt2,
$name, $handle, $pass, $email);
if ($result == -2) echo "The handle '$handle' already exists.";
elseif ($result == 1) echo "User '$name' successfully added.";
else echo "Failed to add user '$name'.";
function PIPHP_AddUserToDB($table, $nmax, $hmax, $salt1, $salt2,
$name, $handle, $pass, $email)
{
// Plug-in 61: Add User To DB
//
// This plug-in adds a user to the selected table
// and database. If the table doesn't exist it
// is created. It takes these arguments:
//
// $table: The table name within $dbname
// $nmax: The max length of $name
// $hmax: The max length of $handle
// $salt1: Characters to obscure $pass
// $salt2: More obscuring characters for $pass
// $name: The user's real name to add to table
// $handle: The user's handle or username
// $pass: The password for $handle
// $email: The email address of $handle
$query = "CREATE TABLE IF NOT EXISTS $table(" .
"name VARCHAR($nmax), handle VARCHAR($hmax), " .
"pass CHAR(32), email VARCHAR(256), " .
"INDEX(name(6)), INDEX(handle(6)), " .
"INDEX(email(6)))";
mysql_query($query) or die(mysql_error());
$query = "SELECT * FROM $table WHERE handle='$handle'";
if (mysql_num_rows(mysql_query($query)) == 1) return -2;
$pass = md5($salt1 . $pass . $salt2);
$query = "INSERT INTO $table VALUES('$name', '$handle', " .
"'$pass', '$email')";
if (mysql_query($query)) return 1;
else return -1;
}
?>