插件61:在数据库中添加用户

本文提供了一个PHP插件示例,用于在MySQL数据库中添加用户,包括创建数据库表、验证用户名是否存在、生成安全口令等关键步骤。
<?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;
}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值