php - Class 'Browscap' not found in - php

I'm using Browscap downloaded from Github.
So I have a _class.php
require_once dirname(__FILE__) . "/core/CORE.php";
require_once dirname(__FILE__) . "/core/Browscap.php";
// use phpbrowscap\Browscap;
class _class extends CORE {
//put your code here
public function __construct($mysql_setup = null) {
parent::__construct($mysql_setup);
$bc = new Browscap(dirname(__FILE__) . "/../cache"); //this is the error line
$get_browser = $bc->getBrowser(null, true);
$get_browser["HTTP_REFERER"] = $_SERVER["HTTP_REFERER"];
$get_browser["REQUEST_TIME"] = date("Y-m-d H:i:s", $_SERVER["REQUEST_TIME"]);
$this->dbCreateTable();
$user = $_SERVER['REMOTE_ADDR'] . ";" . session_id();
if ($this->insertUser($user) !== false) {
$http_user_id = $this->dbLastInsertID();
foreach ($get_browser as $k => $v) {
if (($type_id = $this->getHttpUserTypeDB($k)) !== false) {
$this->dbInsert("http_user_agent_infos", array("info" => $v, "http_user_agent_id" => $http_user_id, "http_user_agent_type_id" => $type_id));
}
}
}
}
My directory hierarchy is like this
C:\xampp\htdocs\test
classes(folder)
core(folder)
Browscap.php
CORE.php
_class.php
form.php
cache(folder)
browscap.ini
cache.php
php_browscap.ini
The error is
Fatal error</b>: Class 'Browscap' not found in <b>C:\xampp\htdocs\job6b\classes\_class.php</b> on line <b>28</b><br />
I'm not sure where I have my error...or did I put the path wrongly?
Thanks

Related

classes/Logger.php / PHP Error

CodeIgniter:
A PHP Error was encountered
Severity: Warning
Message: fopen(scanner/logs/eventlogs_2018-05-06.txt): failed to open stream: No such file or directory
Filename: classes/Logger.php
Logger.php
<?php
class Logger{
private $logFile;
private $fp;
public function lfile($path) {
$this->logFile = $path;
}
public function lwrite($message){
if(!$this->fp)
$this->lopen();
$scriptName = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);
$time = date('H:i:s:ms');
fwrite($this->fp, "$time ,$scriptName, $message\n");
}
private function lopen(){
$lfile = $this->logFile;
$today = date('Y-m-d');
$this->fp = fopen($lfile . '_' . $today . '.txt', 'a') or exit("Can't open $lfile!");
}
}
?>
Bear in mind that my directory is not /scanner/logs/eventlogs/ but its /application/user/views/scanner/ so I have no idea why logger is trying to fopen there... Can anyone help?
I am using this as a form to web scan!
a snippet
$log = new Logger();
$log->lfile('scanner/logs/eventlogs'); // THIS IS WHERE ERROR POPS UP
$log->lwrite('Connecting to database');
$connectionFlag = connectToDb($db);
if(!$connectionFlag)
{
$log->lwrite('Error connecting to database');
echo 'Error connecting to database';
return;
}
You should change this function (which seems to set the path for the other functions):
public function lfile($path) {
$this->logFile = $path;
}
To something like:
public function lfile($path) {
$this->logFile = FCPATH . $path;
}
This way all your paths will be from C:\xampp\htdocs\ (FCPATH example) and not depend on the current working directory where you are calling your function from.
Use the __DIR__ constant, which returns the current directory of the script.
public function lfile($path) {
$this->logFile = __DIR__ . "/" . $path; // sprintf("%s/%s", __DIR__, $path);
}
Learn more: http://php.net/manual/en/language.constants.predefined.php

Class not found in Bootstrap.php in CLI mode (Zend)

I am getting an error when trying to run app in CLI mode. First of all I have daemon controller which allows to run daemon instances (objects with only one required method 'run' derived from my daemon interface) using url /daemon-cli/:Daemon_Class_To_Run. So there is nothing interesting from this side.
Also I have little sh script to run this controller in a simplified way like this:
# ./daemon.sh Ami_Daemon_EventParser
This returns me the next error:
<?
class System_Controller_Plugin_ModuleLayoutLoader extends Zend_Controller_Plugin_Abstract {
/**
* Array of layout paths associating modules with layouts
*/
protected $_moduleLayouts;
// here was the full code of this class
}
PHP Fatal error: Class 'System_Controller_Plugin_ModuleLayoutLoader' not found in /var/www/htdocs/application/Bootstrap.php on line 99
System namespace is located in application/library/System and this is just a set of toolkits and libraries. That is completely weird because it works well in apache2handler mode, but crashes in cli. I don't understand how the class cannot be found if the code of "not founded" class are returned in the error. I suppose I bootstrapped application in a wrong way.
Some of bootstrap code:
protected function _initAppModules()
{
$modules = array();
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('System_');
$includePath = '';
// Add model directories to include path
if ($dhApp = opendir(APPLICATION_PATH . '/modules')) {
while (($dirApp = readdir($dhApp)) !== false) {
if (substr($dirApp, 0, 1) != '.' && is_dir(APPLICATION_PATH . '/modules/' . $dirApp)) {
$modules [] = $dirApp;
if ($dhModule = opendir(APPLICATION_PATH . '/modules/' . $dirApp)) {
while (($dirModule = readdir($dhModule)) !== false) {
if ($dirModule == 'library' && is_dir(APPLICATION_PATH . '/modules/' . $dirApp . '/library')) {
$includePath .= PATH_SEPARATOR . APPLICATION_PATH . '/modules/' . $dirApp . '/library';
$autoloader->registerNamespace(ucfirst($dirApp));
}
}
closedir($dhModule);
}
}
}
closedir($dhApp);
}
ini_set('include_path', ini_get('include_path') . $includePath);
return $modules;
}
protected function _initResources()
{
Zend_Controller_Action_HelperBroker::addPrefix('System_Controller_Action_Helper');
$resourceLoader = new Zend_Loader_Autoloader_Resource(array('basePath' => APPLICATION_PATH . '/library/System', 'namespace' => ''));
$resourceLoader->addResourceTypes(array('form' => array('path' => 'Form/Resource/', 'namespace' => 'Form'), 'model' => array('path' => 'Model/Resource/', 'namespace' => 'Model')));
System_API_Abstract::load();
return $resourceLoader;
}
protected function _initView(array $options = array())
{
$this->bootstrap('AppModules');
$this->bootstrap('FrontController');
$view = new Zend_View();
$view->addScriptPath(APPLICATION_PATH . '/views/scripts');
$view->addHelperPath(APPLICATION_PATH . '/views/helpers', 'View_Helper');
$view->addHelperPath(APPLICATION_PATH . '/modules/gui/views/helpers', 'View_Helper');
$view->addHelperPath(APPLICATION_PATH . '/library/System/View/Helper', 'System_View_Helper');
// next line triggers the error
$layoutModulePlugin = new System_Controller_Plugin_ModuleLayoutLoader();
foreach ($this->getResource('AppModules') as $module) {
if (is_dir(APPLICATION_PATH . '/modules/' . $module . '/views/layouts')) {
$layoutModulePlugin->registerModuleLayout($module, APPLICATION_PATH . '/modules/' . $module . '/views/layouts');
}
if (is_dir(APPLICATION_PATH . '/modules/' . $module . '/views/helpers')) {
$view->addHelperPath(APPLICATION_PATH . '/modules/' . $module . '/views/helpers', ucfirst($module) . '_View_Helper');
}
}
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
$viewRenderer->setView($view);
Zend_Layout::startMvc(array('layoutPath' => '../application/views/layouts', 'layout' => 'layout'));
$this->getResource('FrontController')->registerPlugin($layoutModulePlugin);
return $view;
}
And in the index.php I bootstrap all modules.
if (php_sapi_name() == 'cli') {
$_SERVER['REQUEST_URI'] = $argv[1];
$_SERVER ['HTTP_HOST'] = 'mydomain.info';
ini_set('session.use_cookies', false);
$application->bootstrap()->run();// in case if I find the way to bootstrap partially
} else {
$application->bootstrap()->run();
}
I am fighting with this for a couple of weeks, so I hope somebody has an answer or a good advice. Any help is greatly appreciated. Thanks!
Finally I did that!
As you can see I had a php short tag in the error output. Php often uses separate php.ini for cli mode as my rackspace distributive does. Php distros for windows usually have the common ini file.

spl_autoload_reqister classes not getting loaded

I have a folder structure that looks like
base_dir-
Includes.php
Libs-
Database.php
Log.php
Cofing.php
Models-
someClass.php
Scheduled-
test.php
My Includes.php has
spl_autoload_register(NULL, FALSE);
spl_autoload_extensions('.php, .class.php, lib.php');
function libLoader($name) {
$file = 'Libs/' . $name . '.php';
if (!file_exists($file)) {
// throw new Exception("Error Loading Library: $file does not exists!", 1);
return FALSE;
}
require_once $file;
}
function modelLoader($name) {
$file = 'Models/' . $name . '.php';
if (!file_exists($file)) {
// throw new Exception("Error Loading Library: $file does not exists!", 1);
return FALSE;
}
require_once $file;
}
spl_autoload_register('libLoader');
spl_autoload_register('modelLoader');
My someClass.php has
require_once '../Includes.php';
class someClass extends Database
{
public function __construct() { return 'hello world'; }
}
And test.php has
require_once '../Includes.php';
try {
$loads = new someClass();
} catch (Exception $e) {
echo "Exception: " . $e->getMessage();
}
When I run test.php I get someClass not found on .../Scheduled/test.php
Does spl works with extended classes like someClass.php or do I need to include the class to be exended?
And why it wouldnt find someClass.php?
Thanks
Change
$file = 'Models/' . $name . '.php';
to
$file = __DIR__ . '/Models/' . $name . '.php';
in your models autoloader (and the equivalent in your libLoader) to ensure that it's searching from the correct directory, and not the directory where your test.php file is located

Php autoload project

I've a project like this:
Now i want to autoload all the php files in the folder classes and sub folders.
I can do that with this:
$dirs = array(
CMS_ROOT.'/classes',
CMS_ROOT.'/classes/layout',
CMS_ROOT.'/classes/layout/pages'
);
foreach( $array as $dir) {
foreach ( glob( $dir."/*.php" ) as $filename ) {
require_once $filename;
}
}
But i dont like this. For example.
"layout/pages/a.php" extends "layout/pages/b.php"
Now i get an error because a.php was loaded first. How do you people load your project files? Classes?
SOLVED :)
This is my code now:
spl_autoload_register('autoloader');
function autoloader($className) {
$className = str_replace('cms_', '', $className);
$className = str_replace('_', '/', $className);
$file = CLASSES.'/'.$className.'.php';
if( file_exists( $file ) ) {
require_once $file;
}
}
You should try this
<?php
spl_autoload_register('your_autoloader');
function your_autoloader($classname) {
static $dirs = array(
CMS_ROOT.'/classes',
CMS_ROOT.'/classes/layout',
CMS_ROOT.'/classes/layout/pages'
);
foreach ($dirs as $dir) {
if (file_exists($dir . '/'. $classname. '.php')) {
include_once $dir . '/' . $classname . '.php';
}
}
}
After registering your_autoloader with spl_autoload_register() it will be called by the php interpreter every time you access a class that:
Has not already been loaded with require_once() or include_once()
Is not part of the PHP internals

PHP autoload oddity

function __autoload($class_name) {
echo("Attempting autoload ");
if (substr($class_name, -6) == "Mapper") {
$file = 'mappers/'.$class_name.'.php';
echo "Will autoload $file ";
include_once($file);
}
}
__autoload("UserMapper");
$user = new UserMapper($adapter);
die("done");
Result:
Attempting autoload Will autoload mappers/UserMapper.php done
function __autoload($class_name) {
echo("Attempting autoload ");
if (substr($class_name, -6) == "Mapper") {
$file = 'mappers/'.$class_name.'.php';
echo "Will autoload $file ";
include_once($file);
}
}
//__autoload("UserMapper");
$user = new UserMapper($adapter);
die("done");
(I just commented out the manual call to __autoload()...)
Result:
Fatal error: Class 'UserMapper' not found in C:\Program Files\EasyPHP-5.3.5.0\www\proj\29letters\login.php on line 13
Any ideas?
And yes, I'm running PHP 5.3.5
Not sure why your example isn't working, as it should be as per the manual.
Have you tried using spl_autoload_register to register the autoloader function?
Have you set a proper include_path? You're using a relative path to include the class's file. Try an absolute path instead.
$dir = __DIR__ . '/../path/to/mappers';
$file = $dir . '/' . $class_name . '.php';
require $file;
or
// do this outside of __autoload
set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . '/../path/to/mappers';
// inside __autoload
$file = $class_name . '.php';
require $file;

Categories