Skip to content

Instantly share code, notes, and snippets.

@sergeycherepanov
Last active February 27, 2019 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergeycherepanov/511951b3e61dc2be3a4bd9fda4351419 to your computer and use it in GitHub Desktop.
Save sergeycherepanov/511951b3e61dc2be3a4bd9fda4351419 to your computer and use it in GitHub Desktop.
Loging in under any customer by using the customer_id only
<?php
if (version_compare(phpversion(), '5.2.0', '<')) {
echo 'It looks like you have an invalid PHP version. Magento supports PHP 5.2.0 or newer';
exit;
}
$magentoRootDir = getcwd();
$bootstrapFilename = $magentoRootDir . '/app/bootstrap.php';
$mageFilename = $magentoRootDir . '/app/Mage.php';
if (!file_exists($bootstrapFilename)) {
echo 'Bootstrap file not found';
exit;
}
if (!file_exists($mageFilename)) {
echo 'Mage file not found';
exit;
}
require $bootstrapFilename;
require $mageFilename;
if (!Mage::isInstalled()) {
echo 'Application is not installed yet, please complete install wizard first.';
exit;
}
//if (isset($_SERVER['MAGE_IS_DEVELOPER_MODE'])) {
Mage::setIsDeveloperMode(true);
//}
ini_set('display_errors', 1);
ini_set('memory_limit', '512M');
umask(0);
/* Store or website code */
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';
/* Run store or run website */
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
Mage::app($mageRunCode, $mageRunType);
if (!isset($_REQUEST['id']) || !is_numeric($_REQUEST['id'])) {
echo "Usage: {$_SERVER['REQUEST_URI']}?id=%CUSTOMER_ID%";
die();
}
$customerId = (int)$_REQUEST["id"];
if ($customerId == 0) {
echo "Error: Customer ID should be Integer value only ";
exit;
}
try {
Mage::getSingleton("core/session", array("name" => "frontend"));
$customer = Mage::getModel("customer/customer")->load($customerId);
if (!$customer->getId()) {
echo "Customer ID not found in Magneto : " . $customerId;
exit;
}
/** @var Mage_Customer_Model_Session $session */
$session = Mage::getSingleton('customer/session');
$session->setCustomerAsLoggedIn($customer);
if ($session->isLoggedIn()) {
echo "You was logged in";
} else {
echo "You wasn't logged in";
}
} catch (Mage_Core_Exception $e) {
Mage::throwException($e->getMessage());
}
<?php
namespace mage2login;
use Magento\Customer\Model\ResourceModel\CustomerRepository;
use Magento\Customer\Model\Session;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\State;
if (!isset($_REQUEST['id']) || !is_numeric($_REQUEST['id'])) {
echo "Usage: {$_SERVER['REQUEST_URI']}?id=%CUSTOMER_ID%";
die();
}
require __DIR__ . '/app/bootstrap.php';
\Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$om = ObjectManager::getInstance();
$om->get(State::class)->setAreaCode('frontend');
$session = $om->get(Session::class);
$customer = $om->get(CustomerRepository::class)->getById($_REQUEST['id']);
if ($session->loginById($customer->getId())) {
echo "You was logged in";
} else {
echo "You wasn't logged in";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment