0

I have a php script (Ajax commenting) written in php. It was not written to be a Magento extension. Let us say I want to install it in my site -Magento store-, what are the common practice to do so. In another word, for any third party php script, I want to install these scripts within Magento installation so that I can add Magento codes in the new php script files ( for example, check if Magento user is logged in). I am sure I will need to modify codes in the new script, but these modifications require me to call Magento helper classes. How to install new php script so it has access to Magento classes.

2 Answers 2

1

Writing a shell script is the very best practice but if you need an even quicker solution, here it is:

To init an admin session:

<?php
// Prevent this script to be called via HTTP
if (isset($_SERVER['REQUEST_METHOD']))
{
    die('Permission denied.');
}

// Avoid any time limit
set_time_limit(0);

// Avoid any memory limit
ini_set('memory_limit', -1);

// Include bootstrap code and Mage class
require_once 'app/Mage.php';

// Enable developer mode
Mage::setIsDeveloperMode(true);

// Set the default file creation mask
umask(0);

// Init application with default store
Mage::app();

// Init admin store
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

// Load adminhtml config area
Mage::app()->loadArea(Mage_Core_Model_App_Area::AREA_ADMINHTML);

// Initialize administrator session
$userModel = Mage::getModel('admin/user');
$administrator = $userModel->load(1); // change administrator id here
$adminSession = Mage::getSingleton('admin/session');
$adminSession->renewSession();
$adminSession->setUser($administrator);
$acl = Mage::getResourceModel('admin/acl')->loadAcl();
$adminSession->setAcl($acl);

To init a customer session

<?php
// Prevent this script to be called via HTTP
if (isset($_SERVER['REQUEST_METHOD']))
{
    die('Permission denied.');
}

// Avoid any time limit
set_time_limit(0);

// Avoid any memory limit
ini_set('memory_limit', -1);

// Include bootstrap code and Mage class
require_once 'app/Mage.php';

// Enable developer mode
Mage::setIsDeveloperMode(true);

// Set the default file creation mask
umask(0);

// Init application with default store
Mage::app();

// Load frontend config area
Mage::app()->loadArea(Mage_Core_Model_App_Area::AREA_FRONTEND);

// Initialize customer session
$userModel = Mage::getModel('customer/customer');
$customer = $userModel->load(1); // change customer id here
$customerSession = Mage::getSingleton('customer/session');
$customerSession->setCustomerAsLoggedIn($customer);
$customerSession->renewSession();

Hope it helps.

2
  • Thanks a lot. Is this method described to sort of integrate third party extension libraries with Magento? I got a bit confused with the term shell script. Is there more details, I am new to this. Thanks a lot
    – Husain
    May 21, 2016 at 9:44
  • No, sorry, my code snippets were intended as a replacement for shell script. A shell script is some Magento code executed via the command line interface and not via browser. May 21, 2016 at 16:53
0

Hi

You should write /Shell script. Means check file in magento_root_dir/shell/log.php.

run() function will be call on file run.

if you add code here, and with same method log.php is written, no other people can access your code. because it needs to run from command prompt. so it will need your username and password.

You can use magento default classes because you already have included in start of script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.