Calling OpenCart library functions in a PHP file outside the scope

In the controller, I can easily call a function like this

$this -> user->login('username','password'); 

But out of the box in a separate PHP file, how can I access this method? I tried something like this, but this did not work:

 include($_SERVER['DOCUMENT_ROOT'].'/mydir/opencart/system/library/user.php'); $userobj=new User(); $userobj->login('username','password'); 

Can you help?

Edit / Update: this is what is inside the startup.php file:

 <?php // Error Reporting error_reporting(E_ALL); // Check Version if (version_compare(phpversion(), '5.1.0', '<') == true) { exit('PHP5.1+ Required'); } // Register Globals if (ini_get('register_globals')) { ini_set('session.use_cookies', 'On'); ini_set('session.use_trans_sid', 'Off'); session_set_cookie_params(0, '/'); session_start(); $globals = array($_REQUEST, $_SESSION, $_SERVER, $_FILES); foreach ($globals as $global) { foreach(array_keys($global) as $key) { unset(${$key}); } } } // Magic Quotes Fix if (ini_get('magic_quotes_gpc')) { function clean($data) { if (is_array($data)) { foreach ($data as $key => $value) { $data[clean($key)] = clean($value); } } else { $data = stripslashes($data); } return $data; } $_GET = clean($_GET); $_POST = clean($_POST); $_REQUEST = clean($_REQUEST); $_COOKIE = clean($_COOKIE); } if (!ini_get('date.timezone')) { date_default_timezone_set('UTC'); } // Windows IIS Compatibility if (!isset($_SERVER['DOCUMENT_ROOT'])) { if (isset($_SERVER['SCRIPT_FILENAME'])) { $_SERVER['DOCUMENT_ROOT'] = str_replace('\\', '/', substr($_SERVER['SCRIPT_FILENAME'], 0, 0 - strlen($_SERVER['PHP_SELF']))); } } if (!isset($_SERVER['DOCUMENT_ROOT'])) { if (isset($_SERVER['PATH_TRANSLATED'])) { $_SERVER['DOCUMENT_ROOT'] = str_replace('\\', '/', substr(str_replace('\\\\', '\\', $_SERVER['PATH_TRANSLATED']), 0, 0 - strlen($_SERVER['PHP_SELF']))); } } if (!isset($_SERVER['REQUEST_URI'])) { $_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'], 1); if (isset($_SERVER['QUERY_STRING'])) { $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING']; } } if (!isset($_SERVER['HTTP_HOST'])) { $_SERVER['HTTP_HOST'] = getenv('HTTP_HOST'); } // Helper require_once(DIR_SYSTEM . 'helper/json.php'); require_once(DIR_SYSTEM . 'helper/utf8.php'); // Engine require_once(DIR_SYSTEM . 'engine/action.php'); require_once(DIR_SYSTEM . 'engine/controller.php'); require_once(DIR_SYSTEM . 'engine/front.php'); require_once(DIR_SYSTEM . 'engine/loader.php'); require_once(DIR_SYSTEM . 'engine/model.php'); require_once(DIR_SYSTEM . 'engine/registry.php'); // Common require_once(DIR_SYSTEM . 'library/cache.php'); require_once(DIR_SYSTEM . 'library/url.php'); require_once(DIR_SYSTEM . 'library/config.php'); require_once(DIR_SYSTEM . 'library/db.php'); require_once(DIR_SYSTEM . 'library/document.php'); require_once(DIR_SYSTEM . 'library/encryption.php'); require_once(DIR_SYSTEM . 'library/image.php'); require_once(DIR_SYSTEM . 'library/language.php'); require_once(DIR_SYSTEM . 'library/log.php'); require_once(DIR_SYSTEM . 'library/mail.php'); require_once(DIR_SYSTEM . 'library/pagination.php'); require_once(DIR_SYSTEM . 'library/request.php'); require_once(DIR_SYSTEM . 'library/response.php'); require_once(DIR_SYSTEM . 'library/session.php'); require_once(DIR_SYSTEM . 'library/template.php'); require_once(DIR_SYSTEM . 'library/openbay.php'); require_once(DIR_SYSTEM . 'library/ebay.php'); require_once(DIR_SYSTEM . 'library/amazon.php'); require_once(DIR_SYSTEM . 'library/amazonus.php'); ?> 
+6
source share
1 answer

Instead of this:

 include($_SERVER['DOCUMENT_ROOT'].'/mydir/opencart/system/library/user.php'); 

(including only the user class), enable startup.php , which will load all system classes from OpenCart (it may be undesirable or may run into a project area where you need to use these OpenCart classes, but this may occur if problems occur). You will need to enable OpenCart config.php to make sure that the OpenCart DB class will have the necessary constants to connect to a specific DB. You can do this in a clear way by checking if the files exist in the first place:

 $root = $_SERVER['DOCUMENT_ROOT'] . '/mydir/opencart/'; if (file_exists($root . 'config.php')) { require_once($root . 'config.php'); } if (file_exists($root . 'system/startup.php')) { require_once($root . 'system/startup.php'); } if (file_exists($root . 'system/library/user.php')) { require_once($root . 'system/library/user.php'); } 

Then you can try logging into your account:

 $user = new User(); if ($user->login('username','password')) { echo 'User was logged in successfully'; } else { echo 'User not found or username or password do not match.'; } 

This will be sample code.

Pleaser, see admin/index.php how all the necessary classes are created before the user object is created.

 $registry = new Registry(); $loader = new Loader($registry); $registry->set('load', $loader); $config = new Config(); $registry->set('config', $config); $db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE); $registry->set('db', $db); // ... do the same for rest of required classes ... $user = new User($registry); 
+8
source

Source: https://habr.com/ru/post/974153/


All Articles