In a controller, I can easily call a function like this
$this -> user->login('username','password');
But outside of the framework in a separate PHP file, how can I access that method? I tried something like this but it didn't work:
include($_SERVER['DOCUMENT_ROOT'].'/mydir/opencart/system/library/user.php');
$userobj=new User();
$userobj->login('username','password');
Can you please help?
Edit/Update: Here is what's inside my 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');
?>
Instead of doing this:
include($_SERVER['DOCUMENT_ROOT'].'/mydir/opencart/system/library/user.php');
(including the user
class only), include the startup.php
that will load all the system classes from OpenCart (may be unwanted or may collide with the scope of project where you need to use those OpenCart classes, but this could be fixed when problems occur). You would need to include the OpenCart's config.php
as well to make sure that OpenCart's DB class will have the required constants for connecting to DB defined. You may want to do it in clear way by checking whether the files exist first:
$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 to login your user:
$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.';
}
That would be for a code example.
Pleaser, take a look into admin/index.php
how all the required classes are instantiated prior to creating the User
object.
$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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With