Author Topic: INSERT Login Data Into Multiple Databases?  (Read 2367 times)

Offline Chris Pine

  • Newbie
  • *
  • Posts: 7
  • Karma: +1/-3
    • View Profile
INSERT Login Data Into Multiple Databases?
« on: September 29, 2020, 12:02:44 AM »
Is there a way to 'INSERT' multiple user registrations into the database xf_user so my customers have the ability to login?

I've looked at doing something like:

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
} else {

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'email');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Mary', 'Moe', 'email');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Julie', 'Dooley', 'email')";

if ($conn->multi_query($sql) === TRUE) {
  echo "New records created successfully";
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}

public static function createUser($sUsername, $sEmail, $sPassword, array $aAdditionalData = array()) {

//Create the username from the person's name:
$sUsername = str_replace(' ', "_", $sUsername);

//Set User Data
$cWriter = XenForo_DataWriter::create('XenForo_DataWriter_User');
$cWriter->set('username', $sUsername);
$cWriter->set('email', $sEmail);
$cWriter->setPassword($sPassword);
$cWriter->set('user_group_id', XenForo_Model_User::$defaultRegisteredGroupId);
$cWriter->set('user_state', 'valid');

//echo"<pre>";print_r($cWriter);

foreach ($aAdditionalData AS $data => $key) {
$cWriter->set($data, $key);
}
$cWriter->save();
$cUser = $cWriter->getMergedData();

//Login new user: Log the ip of the user registering
XenForo_Model_Ip::log($cUser['user_id'], 'user', $cUser['user_id'], 'register');
//Set the user back to the browser session
XenForo_Application::get('session')->changeUserId($cUser['user_id']);
XenForo_Visitor::setup($cUser['user_id']);

return $cUser['user_id'];
}

/*
* Get the current user:
*/

public static function getCurrentUser() {

XenForo_Session::startPublicSession();
$cVisitor = XenForo_Visitor::getInstance();
if ($cVisitor->getUserId()) {
$dbUserModel = XenForo_Model::create('XenForo_Model_User');
$cUserInfo = $dbUserModel->getFullUserById($cVisitor->getUserId());
}
return $cUserInfo;
}

/*
* Get the current user:
*/

public static function getUserByEmail($sEmail) {
$dbUserModel = XenForo_Model::create('XenForo_Model_User');
$cUser = $dbUserModel->getUserByEmail($sEmail, array('join' => XenForo_Model_User::FETCH_USER_PROFILE + XenForo_Model_User::FETCH_LAST_ACTIVITY));
return $cUser;
}

/*
* Set the user state: from email_confirm to valid.
*/

public static function setUserState($iXFID, $sState) {
//'valid'
//'email_confirm'
//query("UPDATE xf_user SET user_state = ? WHERE user_id = ? LIMIT 1", array($sState, $iXFID));
}

/*
* Login a XenForo User // Set the cookie.
*/

public static function login($sEmail, $sPassword, $bRemember = true) {

//Get this class; delete existing login information
error_reporting(E_ALL);
restore_error_handler();
restore_exception_handler();

$dbLoginModel = XenForo_Model::create('XenForo_Model_Login');
$dbUserModel = XenForo_Model::create('XenForo_Model_User');
$sError = "";

$iUserID = $dbUserModel->validateAuthentication($sEmail, $sPassword, $sError);
if (!$iUserID) {
$dbLoginModel->logLoginAttempt($sEmail);
return $sError;
}

$dbLoginModel->clearLoginAttempts($sEmail);

if ($bRemember) {
$dbUserModel->setUserRememberCookie($iUserID);
}

XenForo_Model_Ip::log($iUserID, 'user', $iUserID, 'login');

$dbUserModel->deleteSessionActivity(0, $_SERVER['REMOTE_ADDR']);

$cSession = XenForo_Application::get('session');
$cSession->changeUserId($iUserID);
XenForo_Visitor::setup($iUserID);

return $iUserID;
}

/*
* Set this user ID as logged in.
*/

public static function setLogin($iUserID) {
$dbUserModel = XenForo_Model::create('XenForo_Model_User');
$dbUserModel->setUserRememberCookie($iUserID);
XenForo_Model_Ip::log($iUserID, 'user', $iUserID, 'login');
$dbUserModel->deleteSessionActivity(0, $_SERVER['REMOTE_ADDR']);
$cSession = XenForo_Application::get('session');
$cSession->changeUserId($iUserID);
XenForo_Visitor::setup($iUserID);
}

/*
* Check if this user name is in use, return true for already exists.
*/

public static function bUsernameInUse($sUsername) {

$db = XenForo_Application::get('db');

$cUser = $db->fetchRow("SELECT * FROM xf_user WHERE `username` = '$sUsername' LIMIT 1");

if (is_numeric($cUser['user_id'])) {
return true;
} else {
return false;
}
}






Have I missed anything? I cannot figure out how to get multiple databases and login systems to pull off of or store information in each others database. For Example. Login to website one with own db causes an insert of username and password stored into my xf database user and password and makes checks on login whether that username and password are valid and a cookie has been set so within the req timeframe you would be logged in on both websites login systems due to website universal cookie. I know it's possible and I think many of us here are close but could anyone lend a hand here? Thanks so much!
« Last Edit: September 29, 2020, 12:09:38 AM by Chris Pine »

Offline Basara

  • Administrator
  • Hero Member
  • *****
  • Posts: 5774
  • Karma: +274/-2
    • View Profile
Re: INSERT Login Data Into Multiple Databases?
« Reply #1 on: September 29, 2020, 01:52:19 AM »
Hello.

This is AbanteCart forum. Your code is for another application

Offline Chris Pine

  • Newbie
  • *
  • Posts: 7
  • Karma: +1/-3
    • View Profile
Re: INSERT Login Data Into Multiple Databases?
« Reply #2 on: October 03, 2020, 07:38:01 PM »
Absolutely. Instead of recreating database tables, I would rather combine the tables and for things such as 'username', 'email', 'password', and so on.. would just connect and store and call on the same database. So when Xenforo calls for a certain DB then it will check for username 'Chris' and check it's password is correct and log you in. However my main site will use the same thing so when you log into the main website even if you didn't register on it, it will log you in because the credentials match!

Someone tell me why that isn't possible in some variation. Haven't tried yet. Don't feel like destroying my databases yet... even though I guess I can back them up.

Let me know any help is appreciated!

 

Powered by SMFPacks Social Login Mod