PHP included class not updating if modified - php

I create a simple class from a different file and include it
to a page that used to create an object of that class.
Everything works fine, the problems occurs is when I update
the class, I need to manually access the class from my browser
then the page that I create the object will get the latest modified class
or it will receive Error.
below is the page code I use to create an object of the class
<?php
function __autoload($class_name) {
require '/classes/'.$class_name . '.php';
}
$rain = new myClass;
echo $rain->TestMethod(12345,123451);
?>
If I update my class, and without accessing it manually from my browser I will receive this error from my Apache2.
PHP Fatal error: require(): Failed opening required '/classes/myClass.php' (include_path='.:/usr/share/php:/usr/share/pear')

Check if you are using a PHP op cache, or any Apache PHP cache or something that makes your php files not been readead from disk when you change them.
I usually have many problems with shared servers because these modules.
(i see the / is the root folder. It's a common mistake if your classes are not in the root folder of your webserver.)

Related

Loading class in regualr php file in Laravel project

I have a laravel project and in it, I create a regular (.php) file that I am posting some data to. In that file, I want to use a class that I installed using composer in the Laravel project. When I post data to the page I am getting the error No such file in directory and require(): Failed opening required 'vendor/autoload.php. How can I resolve this my project is almost complete if I can get this working.
I've tried: require "class name" but the same error.
PHP file posting to:
<?php
require "vendor/autoload.php";
require 'vendor\lcobucci\jwt\src\Builder.php';
require 'vendor\lcobucci\jwt\src\Signer.php';
//I've tried using these require statements
use \vendor\Lcobucci\JWT\Builder;
use \vendor\Lcobucci\JWT\Signer\Hmac\Sha256;
someFunctiion(){
// do stuff with function from class
}
?>
I expect that when I post to this page, the function from the class I am 'using' will be called but instead, it's saying require() failed to open class.

Including wp-load.php causes fatal error inside a class method

Im doing a direct .php call (ajax). Due to my classes, loading system, I would need to load wp-load later, not at the start.
So, if my file has only this:
include $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
it runs ok. But if its wrapped inside a class:
class Paff
{
public function x()
{
include $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
}
}
(new Paff())->x();
it dies with a fatal error:
PHP Fatal error: Call to undefined method stdClass::escape() in /var/www/wp-content/sunrise.php on line 11
I cant see any reason why!
It seems like you're trying to load a WordPress multisite outside of the normal parameters (judging by sunrise.php being the issue here). Try loading this file instead:
include $_SERVER['DOCUMENT_ROOT'] . '/wp-blog-header.php';
This skips index.php which defines the WP_USE_THEMES constant and assumes you're not loading the front-end of the site (at least not traditionally, which you're not).
That should load everything and in the right order for you now.
I also could not see anything wrong with your code. So I tested it with one change just for confirmation that it does work.
<?php
class Paff
{
public function x()
{
include $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
echo 'wp-load.php included. ABSPATH=' . ABSPATH;
}
}
(new Paff())->x();
?>
It runs without error! and displays the following in the browser.
wp-load.php included. ABSPATH=D:\bronce/
I displayed the ABSPATH just to confirm that wp-load.php was included as it defines ABSPATH.
So this works as it should on my installation which is very standard as I just installed PHP a few weeks ago and I know I didn't change the defaults very much. So why does it not work on your system? It must be configured different. Is your system running any pre-execution handlers?
I just did a search for 'sunrise.php' which I originally thought was the name of your code. It may actually be an infection (or not) see https://premium.wpmudev.org/blog/removing-backdoor-exploits/. I think given its name it is configured to run as a pre-execution handler. Some plugins legitimately install pre-execution handlers, e.g. WordFence. Unfortunately, I did not install WordFence this time and I don't recall the name of its pre-execution handler.
I just read the source code to WordFence. It does install a pre-execution handler but its name is ABSPATH . 'wordfence-waf.php'. However, WordFence also has a reference to '.../wp-content/sunrise.php' apparently executed before multisite loading. Are you using WordFence? Is your site a multisite?
The important point is: The problem is not in your code but in a pre-execution handler. A pre-execution handler can be a huge amount of PHP code that can significantly change the PHP execution environment, which is why plugins such as WordFence use it to monitor your code execution.

Fatal error: Class 'Stripe' not found in C:\wamp\www\

I'm getting an error that class is not found, but I clearly have the right path for where it is located:
<?php
require_once('stripe-php-2.1.0/stripe/lib/Stripe.php');
Stripe::setApiKey('my_key');
var_dump($_POST['stripe-token']);
?>
Every article I've come across all claim that the problem is (not including the right path) in the require_one, include, or require. (I've tried all 3). But still no luck. My database calls follow the same format and my WAMP server has no problem creating my database class.
This is copied directly from my file explore (copy paste)
website\stripe-php-2.1.0\stripe\lib\Stripe.php
My php file that I am using to try and access Stripe sits in the same place as 'website'.
PHP version 5.5.12
tutorial references: http://www.larryullman.com/2013/01/09/writing-the-php-code-to-process-payments-with-stripe/
Other reference: http://www.youtube.com/watch?v=Lka_JBM9bbY
It's because it uses a namespace. Try:
\Stripe\Stripe::setApiKey('my_key');
It is better to initialize all classes.
require_once ("stripe_folder/init.php");
then use namespaces:
\Stripe\Stripe::setApiKey('key_key_key_key_key_key');

Mounting a folder in a phar doesn't work

I've created a phar of a Symfony2 web application, but I'm having some troubles with the cache-folders.
I found out that I could mount an external file/folder into a phar. That would fix my issue, but I can't get the example on the PHP site working.
I have a phar which contains an index.php:
<?php
$configuration = simplexml_load_string(file_get_contents(
Phar::running(false) . '/config.xml'));
?>
Then I include the .phar with the following code:
<?php
// first set up the association between the abstract config.xml
// and the actual one on disk
Phar::mount('phar://config.xml', '/var/www/$projectname/config.xml');
// now run the application
include 'phar-archive.phar';
?>
All files exists, but I get the following error:
PHP Fatal error: Uncaught exception 'PharException' with message 'config.xml is not a phar archive, cannot mount' in /var/www/$projectname/index.php:3
I already tried relative/absolute paths, changing permissions but can't get this to work. Additionally a working example of how I could mount a folder into a phar would be great !
First check that variable $projectname exist in this place (just run echo $projectname; before Phar::mount). If all ok, then change
Phar::mount('phar://config.xml', '/var/www/$projectname/config.xml');
to
Phar::mount('phar://config.xml', "/var/www/{$projectname}/config.xml");
It seems that variable $projectname not converted to its value because you used single quotes.

Using PEAR libraries in custom Magento modules produces "Failed opening required..." error

I have written a Magento module to listen for the "OrderSave" event and perform some API calls to a third party application. Most of the functionality is working great, but I needed to handle an XML response from the API and when I tried to use the PEAR XML_Unserializer class I received the following error:
Fatal error: require_once() [function.require]: Failed opening required 'XML/Parser.php' (include_path='/Users/jeremymoore/Sites/Helm/html/app/code/local:/Users/jeremymoore/Sites/Helm/html/app/code/community:/Users/jeremymoore/Sites/Helm/html/app/code/core:/Users/jeremymoore/Sites/Helm/html/lib:.:/Applications/MAMP/bin/php5/lib/php:/usr/loca/zend//share/ZendFramework/library') in /Users/jeremymoore/Sites/Helm/html/lib/PEAR/XML/Unserializer.php on line 58
My module has an Observer.php file in the model which looks starts as follows:
<?php
require_once 'lib/PEAR/XML/Serializer.php';
require_once 'lib/PEAR/XML/Unserializer.php';
require_once 'lib/Pest/PestXML.php';
Zend_Loader::registerAutoload();
class Helm_Litmos_Model_Observer
{
public function hookToOrderSaveEvent()
{
//Do API Stuff Here
}
}
The hookToOrderSaveEvent functon creates new instances of the serializer and unserializer classes. Before I added the Unserializer code, I had everything working serializing objects and making API calls. It seems that things break down when the XML_Unserializer class tries to reference Parser.php.
I'm not sure that the "require_once" method I'm using here is the appropriate way for me to include these libraries. I'm using Magento 1.4.1.1 which is currently being used on my local machine running MAMP.
Any suggestions on a better way to autoload or include these libraries for use in my module or just ideas on how to fix what I have would be appreciated.
Thanks
You can try and use Mage::getBaseDir('lib')
require_once Mage::getBaseDir('lib').'/PEAR/XML/Serializer.php';
Alan has a good article on Magento's base directories:
http://alanstorm.com/magento_base_directories

Categories