Last active
April 26, 2024 01:52
-
-
Save devops-school/ee5e11c60cac9f48c82cd52f4a3e303a to your computer and use it in GitHub Desktop.
I am creating a form which would take inputs as a email id, username and password but this has to be inserted into Moodle Mysql database and when i login to moodle with the same username and passworde, it should allow login. I understand that moodle uses bcrypt with salt but i m not sure how to write this php code which would be alternate to moo…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Moodle configuration file inclusion - adjust the path as needed | |
require('/path/to/your/moodle/config.php'); | |
// User data | |
$email = 'example@example.com'; // Assume this comes from your form | |
$username = 'newusername'; // Assume this comes from your form | |
$password = 'newpassword'; // Assume this comes from your form, needs to be hashed | |
// Hash the password using bcrypt, compatible with Moodle's password hashing | |
$hashedPassword = password_hash($password, PASSWORD_BCRYPT); | |
// Prepare the new user data | |
$usernew = new stdClass(); | |
$usernew->auth = 'manual'; // Assuming manual authentication | |
$usernew->confirmed = 1; // Active account | |
$usernew->mnethostid = $CFG->mnet_localhost_id; // Main site | |
$usernew->email = $email; | |
$usernew->username = $username; | |
$usernew->password = $hashedPassword; // Use the hashed password | |
$usernew->firstname = 'Firstname'; // You may want to collect this in your form | |
$usernew->lastname = 'Lastname'; // You may want to collect this in your form | |
$usernew->timecreated = time(); | |
$usernew->timemodified = time(); | |
// Insert user into the database | |
$userId = $DB->insert_record('user', $usernew); | |
echo "User created with ID: ".$userId; | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once('config.php'); // Adjust the path as necessary to point to Moodle's config.php | |
global $DB, $CFG; | |
require_once($CFG->dirroot.'/lib/weblib.php'); | |
require_once($CFG->dirroot.'/user/lib.php'); | |
require_once($CFG->dirroot.'/lib/moodlelib.php'); | |
// Replace these with the values from your form | |
$email = 'newuser@example.com'; | |
$username = 'newusername'; | |
$password = 'newpassword'; // Ensure this is appropriately secured in your form | |
// Prepare user new object | |
$usernew = new stdClass(); | |
$usernew->auth = 'manual'; // Assuming manual authentication | |
$usernew->confirmed = 1; // Set to 1 to confirm user automatically | |
$usernew->mnethostid = $CFG->mnet_localhost_id; // Main site | |
$usernew->email = $email; | |
$usernew->username = $username; | |
$usernew->password = hash_internal_user_password($password); // Hash password | |
$usernew->firstname = 'Firstname'; // Adjust as necessary | |
$usernew->lastname = 'Lastname'; // Adjust as necessary | |
// Additional fields like city, country, etc., should be set according to your needs | |
try { | |
$user_id = user_create_user($usernew); // Create the user | |
echo "User created with ID: " . $user_id; | |
} catch (Exception $e) { | |
// Handle error | |
echo "Error creating user: " . $e->getMessage(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment