6

我最近发现这个名为 usercake (http://usercake.com/) 的小用户类脚本具有所有基本功能,并且看起来运行良好。

我的问题:第一个用户可以很好地添加到数据库中,但之后它就无法正常工作了。显然,我没有弄清楚只是有些小问题(我不太了解 oop php)。没有错误发生(我可以看到),并且电子邮件被发送出去。

我已经以相同的命运将它安装在多个地方。我想修复它,因为使用此脚本可以节省大量重新发明轮子的时间。

这是我拥有它的 URL:http ://rawcomposition.com/birding/loggedin/register.php 这是验证所有内容后调用的函数:

    public function userCakeAddUser()
{
    global $db,$emailActivation,$websiteUrl,$db_table_prefix;

    //Prevent this function being called if there were construction errors
    if($this->status)
    {
        //Construct a secure hash for the plain text password
        $secure_pass = generateHash($this->clean_password);

        //Construct a unique activation token
        $this->activation_token = generateActivationToken();

        //Do we need to send out an activation email?
        if($emailActivation)
        {
            //User must activate their account first
            $this->user_active = 0;

            $mail = new userCakeMail();

            //Build the activation message
            $activation_message = lang("ACTIVATION_MESSAGE",array($websiteUrl,$this->activation_token));

            //Define more if you want to build larger structures
            $hooks = array(
                "searchStrs" => array("#ACTIVATION-MESSAGE","#ACTIVATION-KEY","#USERNAME#"),
                "subjectStrs" => array($activation_message,$this->activation_token,$this->unclean_username)
            );

            /* Build the template - Optional, you can just use the sendMail function 
            Instead to pass a message. */
            if(!$mail->newTemplateMsg("new-registration.txt",$hooks))
            {
                $this->mail_failure = true;
            }
            else
            {
                //Send the mail. Specify users email here and subject. 
                //SendMail can have a third parementer for message if you do not wish to build a template.

                if(!$mail->sendMail($this->clean_email,"New User"))
                {
                    $this->mail_failure = true;
                }
            }
        }
        else
        {
            //Instant account activation
            $this->user_active = 1;
        }   


        if(!$this->mail_failure)
        {
                //Insert the user into the database providing no errors have been found.
                $sql = "INSERT INTO `".$db_table_prefix."Users` (
                        `Username`,
                        `Username_Clean`,
                        `Password`,
                        `Email`,
                        `ActivationToken`,
                        `LastActivationRequest`,
                        `LostPasswordRequest`, 
                        `Active`,
                        `Group_ID`,
                        `SignUpDate`,
                        `LastSignIn`
                        )
                        VALUES (
                        '".$db->sql_escape($this->unclean_username)."',
                        '".$db->sql_escape($this->clean_username)."',
                        '".$secure_pass."',
                        '".$db->sql_escape($this->clean_email)."',
                        '".$this->activation_token."',
                        '".time()."',
                        '0',
                        '".$this->user_active."',
                        '1',
                        '".time()."',
                        '0'
                        )";

            return $db->sql_query($sql);
        }
    }
}

这是表结构:

CREATE TABLE IF NOT EXISTS `userCake_Users` (
  `User_ID` int(11) NOT NULL AUTO_INCREMENT,
  `Username` varchar(150) NOT NULL,
  `Name` varchar(100) NOT NULL,
  `Username_Clean` varchar(150) NOT NULL,
  `Password` varchar(225) NOT NULL,
  `Email` varchar(150) NOT NULL,
  `ActivationToken` varchar(225) NOT NULL,
  `LastActivationRequest` int(11) NOT NULL,
  `LostPasswordRequest` int(1) NOT NULL DEFAULT '0',
  `Active` int(1) NOT NULL,
  `Group_ID` int(11) NOT NULL,
  `SignUpDate` int(11) NOT NULL,
  `LastSignIn` int(11) NOT NULL,
  PRIMARY KEY (`User_ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
4

3 回答 3

2

对我来说,在添加第一个用户后没有添加更多用户的原因有两种:

首先,$this->mail_failure在创建第一个用户后,后续用户帐户的 flag 设置为 TRUE。但这种情况不太可能发生,因为它是为第一个用户成功运行的相同代码,因此没有理由让其他人的标志为 TRUE。

第二种可能性是$this->status第二个用户帐户为 FALSE。如果为 false,则该方法userCakeAddUser()不执行任何操作。此标志可能为假的原因是用户名或电子邮件地址已经存在。

您是否也在为第二个帐户使用与第一个帐户相同的用户名或电子邮件地址?我确定您不能使用相同的用户名,但可能使用相同的电子邮件地址。usercake 类不允许使用相同的用户名或相同的电子邮件地址。

希望这可以帮助。

于 2011-06-22T09:36:51.080 回答
2

我会用这个丑陋的代码做 4 件事:

1) 启用 error_reporting 模式,以便在发生某事时可以看到一些内容:

 error_reporting(E_ALL);  

2) 直接测试这个 INSERT sql到 dB以确保它工作正常,并验证这段代码。如果 sql INSERT 请求有效,那么检查这些 SQL 请求的访问条件,就像上面 Abhay 说的,

3) 由于我们没有您的所有配置,猜谜游戏很困难。因此,我建议您为 AI User_ID 添加一个 NULL 字段。

$sql = "INSERT INTO `".$db_table_prefix."Users` (
                    `User_ID`, // Add this here
                    `Username`,
                    `Username_Clean`,
                    `Password`,
                    `Email`,
                    `ActivationToken`,
                    `LastActivationRequest`,
                    `LostPasswordRequest`, 
                    `Active`,
                    `Group_ID`,
                    `SignUpDate`,
                    `LastSignIn`
                    )
                    VALUES (
                    NULL,  // and that one
                    '".$db->sql_escape($this->unclean_username)."',
                    '".$db->sql_escape($this->clean_username)."',
                    '".$secure_pass."',
                    '".$db->sql_escape($this->clean_email)."',
                    '".$this->activation_token."',
                    '".time()."',
                    '0', // later, I would also try using an int for an int
                    '".$this->user_active."',
                    '1',
                    '".time()."',
                    '0'
                    )";

4) 使用 OOP 和 PDO 找到另一个更好的编码。

于 2011-06-22T23:03:19.493 回答
0

您将 Name 指定为 NOT NULL 并且在代码的 Insert 语句中未发送 Name 值,因此 mysql 将抛出异常,说 Name 不能为空,请检查一次。

于 2011-06-22T06:27:23.790 回答