PHP script to create user

Started by cantercrow, August 06, 2023, 04:46:03 PM

Previous topic - Next topic

cantercrow

Hi,

I have the script below to create a user but when I post to it it does not error but also does not create the uesr, what am I doing wrong?

define('SMF', true);

if (!defined('SMF'))
{
    die('Hacking attempt');
}

if (isset($_POST['username']) && isset($_POST['password']))
{
    require_once 'SSI.php';
    require_once 'Sources/Subs-Members.php';

echo "Post OK";

    $username = $_POST['username'];
    $password = $_POST['password'];

    // Create a new user
    $email = $username . '@example.com';
    $memberOptions = array(
        'password' => $password,
        'username' => $username,
        'email' => $email,
        'timeRegistered' => time()
);


    $memberID = registerMember($memberOptions);
                       
if ($memberID == 0)
    {
        echo "Error registering user.";
        exit;
    }
    else
    {
        echo "User successfully registered.";
        exit;
    }
}
                       

else

{
    echo "Missing username or password.";
    exit;
}
?>


Tyrsson

Quote from: cantercrow on August 06, 2023, 04:46:03 PMHi,

I have the script below to create a user but when I post to it it does not error but also does not create the uesr, what am I doing wrong?

define('SMF', true);

if (!defined('SMF'))
{
    die('Hacking attempt');
}

if (isset($_POST['username']) && isset($_POST['password']))
{
    require_once 'SSI.php';
    require_once 'Sources/Subs-Members.php';

echo "Post OK";

    $username = $_POST['username'];
    $password = $_POST['password'];

    // Create a new user
    $email = $username . '@example.com';
    $memberOptions = array(
        'password' => $password,
        'username' => $username,
        'email' => $email,
        'timeRegistered' => time()
);


    $memberID = registerMember($memberOptions);
                       
if ($memberID == 0)
    {
        echo "Error registering user.";
        exit;
    }
    else
    {
        echo "User successfully registered.";
        exit;
    }
}
                       

else

{
    echo "Missing username or password.";
    exit;
}
?>

This is the function in question:
https://github.com/SimpleMachines/SMF/blob/bfc48eef2089ec47cc004e66973124d291d599ca/Sources/Subs-Members.php#L434C12-L434C12
Try something like this:
$memberID = registerMember($memberOptions, true); // true is so it will return errors
if( is_int($memberID) && $memberID > 0) {
   // should have a member
} elseif(is_array($memberID)) {
   // error since it returned an array
}
PM at your own risk, some I answer, if they are interesting, some I ignore.

Advertisement: